protected void btnUpdate_Click(object sender, EventArgs e) { //Session["cart"] = (List<Models.CartItem>)GridView1.DataSource; List <Models.CartItem> cart = new List <Models.CartItem>(); for (int i = 0; i < GridView1.Rows.Count; i++) { Models.CartItem ci = new Models.CartItem(); ci.ProductId = Convert.ToInt32(((Label)GridView1.Rows[i].FindControl("lblProductId")).Text); ci.Product = ((Label)GridView1.Rows[i].FindControl("lblProduct")).Text; ci.Qty = Convert.ToInt32(((TextBox)GridView1.Rows[i].FindControl("txtQty")).Text); ci.Rate = Convert.ToInt32(((Label)GridView1.Rows[i].FindControl("lblRate")).Text); cart.Add(ci); } Session["cart"] = cart; GridView1.DataSource = cart; GridView1.DataBind(); Label lbl = (Label)GridView1.FooterRow.FindControl("lblTotal"); lbl.Text = ((List <Models.CartItem>)Session["cart"]).Sum(ci => ci.SubTotal).ToString(); }
protected void btnAdd_Click(object sender, EventArgs e) { Button btn = (Button)sender; List <Models.CartItem> cart = (List <Models.CartItem>)Session["cart"]; int productId = Convert.ToInt32(btn.CommandArgument); if (btn.Text == "Add") { btn.Text = "Remove"; DAL.Product p = new DAL.Product(); p.Id = productId; p.selectById(); Models.CartItem ci = new Models.CartItem(); ci.ProductId = productId; ci.Product = p.Name; ci.Qty = 1; ci.Rate = (float)p.Price; cart.Add(ci); } else { btn.Text = "Add"; var v = cart.Where(ci => ci.ProductId == productId).First(); cart.Remove(v); } Session["cart"] = cart; }