protected void UpdateQuantityBtn_Click(object sender, EventArgs e) { int rowsCount = grid.Rows.Count; int newQuantity; string productID; int prodID; GridViewRow gridRow; TextBox qtyTextBox; bool success = true; for (int i = 0; i < rowsCount; i++) { gridRow = grid.Rows[i]; productID = grid.DataKeys[i].Value.ToString(); qtyTextBox = (TextBox)gridRow.FindControl("EditQty"); if (Int32.TryParse(qtyTextBox.Text, out newQuantity)) { success = success && ShoppingCartAccess.UpdateItem(productID, newQuantity); } else { success = false; } } statusLabel.Text = success ? "Your cart was successfully updated." : "Update Failed, Please check your cart."; PopulateControls(); }
protected void AddToCart_Button_Click(object sender, EventArgs e) { string prodID = Request.QueryString["ProductID"]; string cartID; HttpContext context = HttpContext.Current; if (context.Request.Cookies["NT_CartID"] != null) { cartID = context.Request.Cookies["NT_CartID"].Value; } else { cartID = Guid.NewGuid().ToString(); HttpCookie cookie = new HttpCookie("NT_CartID", cartID); DateTime currentDate = DateTime.Now; TimeSpan timeSpan = new TimeSpan(10, 0, 0); DateTime experiationDate = currentDate.Add(timeSpan); cookie.Expires = experiationDate; context.Response.Cookies.Add(cookie); } ShoppingCartAccess.AddItem(cartID, prodID); }
protected void PopulateControls() { DataTable dt = ShoppingCartAccess.GetCartItems(); if (dt.Rows.Count > 0) { Label1.Text = "These are the items in your cart: "; grid.DataSource = dt; grid.DataBind(); decimal amount = ShoppingCartAccess.GetCartTotal(); cartTotal.Text = String.Format("{0:c}", amount); UpdateQuantityBtn.Enabled = true; } else { Label1.Text = "These are no items in your cart "; grid.Visible = false; grid.DataBind(); cartTotal.Text = String.Format("{0:c}", 0); UpdateQuantityBtn.Enabled = false; } }