list using Session :
Class Create :
public class CartList
{
public int quantity;
public string Name;
public decimal amount;
public int ID;
public string ImageURL;
public string Price;
}
Using C# Page :
List<CartList> objCartList;
protected void btn_Click(object sender, EventArgs e)
{
/* Create Cart List */
bool isNew = true;
if ((Session["Cart"] == null))
objCartList = new List<CartList>();
else
objCartList = (List<CartList>)Session["cart"];
/* ID already Checked.alredy ProductID to Add list Update amount and Quantity */
for (int i = 0; i < objCartList.Count; i++)
{
if (objCartList[i].ID == Convert.ToInt16(lblID.Text.ToString()))
{
// objCartList.Remove(objCartList[i]);
objCartList[i].amount = objCartList[i].amount +
Convert.ToDecimal(lblPrice.Text);
objCartList[i].quantity = objCartList[i].quantity + 1;
isNew = false;
break;
}
}
/* Insert New List */
if (isNew)
{
CartList cartList = new CartList();
cartList.ID = Convert.ToInt16(lblID.Text);
cartList.Name = lblName.Text;
cartList.amount = Convert.ToDecimal(lblPrice.Text);
cartList.quantity = 1;
cartList.ImageURL = Image1.ImageUrl.ToString();
cartList.Price = lblPrice.Text;
objCartList.Add(cartList);
// objCartList.Sort();
}
Session["Cart"] = objCartList;
string[] arrID = new string[20];
for (int i = 0; i < objCartList.Count; i++)
{
arrID[i] =Convert.ToInt16(objCartList[i].quantity).ToString();
bb += Convert.ToInt16(arrID[i]);
}
string cc = bb.ToString();
Session["ItemCount"] = cc.ToString();
}
another C# Page :
aspx:
<asp:Repeater ID="rptProductList" runat="server" OnItemCommand="rptProductList_ItemCommand" >
<HeaderTemplate>
<div style="border:1px solid #6f6e6e">
<table>
<tr style="background:#6f6e6e;height:25px">
<td class="tblTD"></td>
<td class="tblTD">
<asp:Label ID="lbl" Text="ProductName" runat="server"> </asp:Label>
</td>
<td class="tblTD">
<asp:Label ID="Label1" Text="Price" runat="server"> </asp:Label>
</td>
<td class="tblTD">
<asp:Label ID="Label2" Text="Quantity" runat="server"> </asp:Label>
</td>
<td class="tblTD">
<asp:Label ID="Label3" Text="Total Price" runat="server"> </asp:Label>
</td>
<td class="tblTD">
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<div>
<tr>
<td class="tblTD">
<asp:Image ID="Image1" runat="server" Height="125px" Width="125px" />
</td>
<td class="tblTD" style="color:#6f6e6e">
<asp:Label ID="lblName" runat="server"></asp:Label>
</td>
<td class="tblTD" style="color:#6f6e6e">
<asp:Label ID="lblPrice" runat="server"></asp:Label>
</td>
<td class="tblTD" style="color:#6f6e6e">
<asp:TextBox ID="txtQuantity" Width="50px" MaxLength="3" runat="server"></asp:TextBox>
--Update Column
<asp:LinkButton ID="lnkUpdate" runat="server" Text="Update" CommandName="UpdateColumn">
</asp:LinkButton>
</td>
<td class="tblTD" style="color:#6f6e6e">
<asp:Label ID="lblQuantity" runat="server"></asp:Label>
</td>
<td class="tblTD" style="color:#6f6e6e">
<asp:LinkButton ID="lnkDelete" runat="server" Text="X"
CommandName="DeleteColumn">
</asp:LinkButton>
<asp:Label ID="lblID" runat="server"></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</div>
</FooterTemplate>
</asp:Repeater>
aspx:cs
List<CartList> objCartList;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
/* Session Cart Using List */
objCartList = (List<CartList>)Session["cart"];
View();
}
}
public void View()
{
string[] arrID = new string[20];
int Count;
if (objCartList.Count != 0)
{
/* Repeater Cintrol Fill using Session Cart */
foreach (RepeaterItem rpt in rptProductList.Items)
{
Label lbl = (Label)rpt.FindControl("lblQuantity");
Label lblPri = (Label)rpt.FindControl("lblPrice");
Label lblID = (Label)rpt.FindControl("lblID");
TextBox txtQuantity = (TextBox)rpt.FindControl("txtQuantity");
Label lblName = (Label)rpt.FindControl("lblName");
Image img = (Image)rpt.FindControl("Image1");
LinkButton lnkUpda = (LinkButton)rpt.FindControl("lnkUpdate");
LinkButton lnkDel = (LinkButton)rpt.FindControl("lnkDelete");
lnkUpda.CommandArgument=
objCartList[rpt.ItemIndex].ID.ToString();
lnkDel.CommandArgument =
objCartList[rpt.ItemIndex].ID.ToString();
img.ImageUrl =
objCartList[rpt.ItemIndex].ImageURL.ToString();
lbl.Text =
objCartList[rpt.ItemIndex].amount.ToString();
lblPri.Text =
objCartList[rpt.ItemIndex].Price.ToString();
txtQuantity.Text =
objCartList[rpt.ItemIndex].quantity.ToString();
lblName.Text =
objCartList[rpt.ItemIndex].Name.ToString();
}
}
else
{
lblCartNotselected.Text = "You Selected Not Cart";
Session["ItemCount"] = "0";
}
}
/* Repeater Command Using Update Delete CartList Row */
protected void rptProductList_ItemCommand(object source, RepeaterCommandEventArgs e)
{
// btn.Attributes.Add("onclick", "if
//( ! confirm( 'Delete this record?' )) return false; ");
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
--DeleteColumn
if (e.CommandName == "DeleteColumn")
{
bjCartList = (List<CartList>)Session["cart"];
UpdateID = e.CommandArgument.ToString();
TextBox txtQuantity = (TextBox)e.Item.FindControl("txtQuantity");
for (int i = 0; i < objCartList.Count; i++)
{
if (objCartList[i].ID == Convert.ToInt16(UpdateID))
{
--Remove
objCartList.Remove(objCartList[i]);
break;
}
}
View();
string[] arrID = new string[20];
for (int i = 0; i < objCartList.Count; i++)
{
arrID[i] =
Convert.ToInt16(objCartList[i].quantity).ToString();
bb += Convert.ToInt16(arrID[i]);
}
string cc = bb.ToString();
Session["ItemCount"] = cc.ToString();
}
--UpdateColumn
if (e.CommandName == "UpdateColumn")
{
objCartList = (List<CartList>)Session["cart"];
UpdateID = e.CommandArgument.ToString();
TextBox txtQuantity = (TextBox)e.Item.FindControl("txtQuantity");
Label lblPri = (Label)e.Item.FindControl("lblPrice");
for (int i = 0; i < objCartList.Count; i++)
{
if (objCartList[i].ID == Convert.ToInt16(UpdateID))
{
--Update
int aa = Convert.ToInt16(txtQuantity.Text);
int bb = Convert.ToInt16(lblPri.Text);
objCartList[i].quantity = aa;
objCartList[i].amount = aa * bb;
break;
}
}
View();
string[] arrID = new string[20];
for (int i = 0; i < objCartList.Count; i++)
{
arrID[i] = Convert.ToInt16(objCartList[i].quantity).ToString();
bb += Convert.ToInt16(arrID[i]);
}
string cc = bb.ToString();
Session["ItemCount"] = cc.ToString();
}