private void LoadCart() { List<Cart> carts = (List<Cart>)Session["Carts"]; Cart cart = new Cart(carts); grvViewCart.DataKeyNames = new string[] { "ProductID" }; grvViewCart.DataSource = carts; grvViewCart.DataBind(); cart = new Cart(carts); lbTotalQuantity.Text = ToSQL.EmptyNull(cart.TotalQuantity); lbTotalPrice.Text = ToSQL.EmptyNull(cart.TotalPrice.ToString("$#,###.##")); }
protected void rptProducts_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandName == "Add") { HiddenField hdf = (HiddenField)e.Item.FindControl("hdfProductId"); Product product = new ProductRepo().GetById(ToSQL.SQLToInt(hdf.Value)); if (product != null) { List<Cart> carts = (List<Cart>)Session["Carts"]; Cart cart = new Cart(carts); cart = cart.ConverProductToCart(product); carts = cart.Add(cart); Session["Carts"] = carts; Response.Redirect("ViewCart.aspx"); } } else if (e.CommandName == "AddCompare") { CompareAndWish list = (CompareAndWish)Session["Compare"]; if (list == null) list = new CompareAndWish(); Product p = (new ProductRepo()).GetById(ToSQL.SQLToInt(e.CommandArgument)); if (p != null && list.Products.Count <= 3) { if (list.Add(p)) Response.Write("<script type='text/javascript'>alert('Added!s');</script>"); else Response.Write("<script type='text/javascript'>alert('Product is exist in list compare');</script>"); } else { Response.Write("<script type='text/javascript'>alert('You should select between 1 and 4 item!');</script>"); } UpdateCompareList(list.Products); Session["Compare"] = list; } else if (e.CommandName == "AddWishList") { CompareAndWish list = (CompareAndWish)Session["WishList"]; if (list == null) list = new CompareAndWish(); Product p = (new ProductRepo()).GetById(ToSQL.SQLToInt(e.CommandArgument)); if (p != null) { if (!list.Add(p)) Response.Write("<script type='text/javascript'>alert('Product is exist in list compare');</script>"); } UpdateWishList(list.Products); Session["WishList"] = list; } }
protected void grvViewCart_RowUpdating(object sender, GridViewUpdateEventArgs e) { //TextBox txtQuantity = (TextBox)grvViewCart.Rows[e.RowIndex].Cells[1].Controls[0]; TextBox txtQuantity = (TextBox)grvViewCart.Rows[e.RowIndex].Cells[1].FindControl("txtQuantity"); List<Cart> carts = (List<Cart>)Session["Carts"]; int ProductID = ToSQL.SQLToInt(grvViewCart.DataKeys[e.RowIndex]["ProductID"]); Cart cart = new Cart(carts); if (ToSQL.SQLToInt(txtQuantity.Text) > 0) carts = cart.Update(ProductID, ToSQL.SQLToInt(txtQuantity.Text)); Session["Carts"] = carts; grvViewCart.EditIndex = -1; LoadCart(); }
protected void grvViewCart_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Remove") { List<Cart> carts = (List<Cart>)Session["Carts"]; for (int i = 0; i < grvViewCart.Rows.Count; i++) { CheckBox chkRemove = (CheckBox)grvViewCart.Rows[i].FindControl("chkRemove"); if (chkRemove.Checked) { int ProductID = (int)grvViewCart.DataKeys[i]["ProductID"]; Cart cart = new Cart(carts); carts = cart.Remove(ProductID); } } Session["Carts"] = carts; LoadCart(); } }
private void Init() { List<Cart> carts = new List<Cart>(); Cart cart = new Cart(carts); cart.ProductID = 1; cart.ProductName = "Dell"; cart.Price = 4.36M; carts = cart.Add(cart); cart = new Cart(carts); cart.ProductID = 2; cart.ProductName = "Asus"; cart.Price = 1.36M; carts = cart.Add(cart); cart = new Cart(carts); cart.ProductID = 3; cart.ProductName = "Acer"; cart.Price = 2.36M; carts = cart.Add(cart); Session["Carts"] = carts; }
protected void lbtnAddCart_Click(object sender, EventArgs e) { LinkButton lbtn = (LinkButton)sender; Product product = new ProductRepo().GetById(ToSQL.SQLToInt(lbtn.CommandArgument)); if (product != null) { List<Cart> carts = (List<Cart>)Session["Carts"]; Cart cart = new Cart(carts); cart = cart.ConverProductToCart(product); carts = cart.Add(cart); Session["Carts"] = carts; Response.Redirect("ViewCart.aspx"); } }
private bool AddCart() { Product product = new ProductRepo().GetById(ToSQL.SQLToInt(Request.QueryString["ProductId"])); if (product != null) { List<Cart> carts = (List<Cart>)Session["Carts"]; Cart cart = new Cart(carts); cart = cart.ConverProductToCart(product); cart.Quantity = ToSQL.SQLToInt(txtQuantity.Text); carts = cart.Add(cart); //int Quantity = ToSQL.SQLToInt(txtQuantity.Text) - 1; //if (Quantity > 0) //{ // var obj = carts.FirstOrDefault(x => x.ProductID == cart.ProductID); // if (obj != null) obj.Quantity = obj.Quantity + Quantity; //} Session["Carts"] = carts; return true; } return false; }
public List<Cart> Add(Cart cart) { var obj = Carts.FirstOrDefault(x => x.ProductID == cart.ProductID); if (obj != null) obj.Quantity = obj.Quantity + cart.Quantity; else Carts.Add(cart); return Carts; }