/// <summary> /// GridView更新按钮处理事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void gvCart_RowUpdating(object sender, GridViewUpdateEventArgs e) { ShoppingManager manager = new ShoppingManager(Session["Cart"]); foreach (GridViewRow dr in this.gvCart.Rows) { Label lblBookId = this.gvCart.Rows[e.RowIndex].FindControl("lblBookId") as Label; TextBox txtQuantity = this.gvCart.Rows[e.RowIndex].FindControl("txtQuantity") as TextBox; int bookId = int.Parse(lblBookId.Text); int quantity = int.Parse(txtQuantity.Text); manager.UpdateQuantity(bookId, quantity); } Session["Cart"] = manager.ShoppingItems; this.gvCart.EditIndex = -1; this.gvCart.DataSource = manager.ShoppingItems; this.gvCart.DataBind(); this.ltrSalary.Text = string.Format("{0:F}", manager.TotalPrice); }
/// <summary> /// 将书籍添加到购物车 /// </summary> /// <param name="item"></param> /// <returns></returns> public ActionResult UpdateQuantity(ShoppingItem item) { if (ModelState.IsValid) { if (Session["CurrentUser"] == null) { return(Content("<script>alert('会话已失效,请重新登录!');document.location='" + Url.Action("Login", "Account", new { ReturnUrl = Server.UrlDecode(Request.RawUrl) }) + "';</script>")); } else { ShoppingManager manager = new ShoppingManager(Session["Cart"]); manager.UpdateQuantity(item.Book.Id, item.Quantity); Session["Cart"] = manager.ShoppingItems; } } // return RedirectToAction("Index"); return(Index()); }