// click add to cart private void ClickAddToCart(object _sender, EventArgs _e) { // get cart ShoppingCart_S1 _cart = (ShoppingCart_S1)((ShopMaster)Master).GetCart(); // get label for response Label _response = (Label)contentItemArea.FindControl("LabelCartAdded"); if (_response == null) { Trace.Warn("Shop", "Failed to find label for cart response"); return; } // add item if (_cart.Add(item.ID)) { _response.Text = string.Format("Added to cart"); _response.ForeColor = System.Drawing.Color.YellowGreen; Response.Redirect("Cart.aspx", true); } else { _response.Text = string.Format("Failed to add to cart"); _response.ForeColor = System.Drawing.Color.Red; } }
// updates table private void RefreshCartTable() { // get cart ShoppingCart_S1 _cart = (ShoppingCart_S1)((ShopMaster)Master).GetCart(); // cycle through each row, except the first one // find items w/ quantity of zero, remove row TableRowCollection _rows = tableCart.Rows; int i = 1; while (i < _rows.Count) { TableRow _row = _rows[i]; // get ID for row HyperLink _link = (HyperLink)_row.Cells[0].Controls[0]; int _id = Int32.Parse(_link.Text); int _actualQuantity = _cart.Quantity(_id); // check quantity if (_actualQuantity <= 0) { _row.Cells.Clear(); _rows.Remove(_row); Trace.Warn("cart", string.Format("removed row id: {0}", _id)); } else { // check if quantity changed int _quantity = Int32.Parse((_row.Cells[3].Text)); if (_actualQuantity != _quantity) { _row.Cells[3].Text = _actualQuantity.ToString(); } ++i; } } // check row count if (tableCart.Rows.Count <= 1) { // empty table tableCart.Rows.Clear(); labelCart.Text = "Cart is empty"; labelCart.ForeColor = System.Drawing.Color.OrangeRed; tableCartContainer.Visible = false; } }
// session methods // get shopping cart public object GetCart() { // get cart from session state ShoppingCart_S1 _cart = (ShoppingCart_S1)Session["Cart"]; // if does not exist, create if (_cart == null) { _cart = new ShoppingCart_S1(); Session.Add("Cart", _cart); } return(_cart); }
// remove button click private void CommandRemove(object _sender, CommandEventArgs _e) { Trace.Warn("cart", string.Format("CommandRemove(\"{0}\":{1})", _e.CommandName, _e.CommandArgument)); if (_e.CommandName.Equals("ItemID", StringComparison.OrdinalIgnoreCase)) { int _id = Int32.Parse((string)_e.CommandArgument); ShoppingCart_S1 _cart = (ShoppingCart_S1)((ShopMaster)Master).GetCart(); if (_cart.Remove(_id, 1)) { Trace.Warn("cart", string.Format("removed id:{0}", _id)); } else { Trace.Warn("cart", string.Format("failed to remove id:{0}", _id)); } } }
// builds cart table private void BuildCartTable() { // get cart from master page ShoppingCart_S1 _cart = (ShoppingCart_S1)((ShopMaster)Master).GetCart(); // get items IReadOnlyCollection <ShoppingCart_S1.ShoppingCartItem_S1> _items = _cart.Items; // check if empty if (_items.Count > 0) { PopulateTable(_items); } else { // build message labelCart.Text = "Cart is empty"; labelCart.ForeColor = System.Drawing.Color.OrangeRed; tableCartContainer.Visible = false; } }