protected void BindCartInfo() { BLL.CartManager carBll = new BookShop.BLL.CartManager(); List <Model.Cart> list = carBll.GetModelList("UserId=" + userModel.Id); if (list.Count < 1) { Response.Redirect("/showinfo.aspx?msg=" + Server.UrlEncode("没有商品项,无法下订单") + "&url=/BookList.aspx" + "&txt=" + Server.UrlEncode("返回商品列表")); } else { StringBuilder builder = new StringBuilder(); foreach (Model.Cart model in list) { builder.Append("<tr class=\"align_Center\">"); builder.Append("<td style=\"PADDING-BOTTOM: 5px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; PADDING-TOP: 5px\">" + model.Book.Id + "</td>"); builder.Append(" <td class=align_Left><a onmouseover=\"\" onmouseout=\"\" onclick=\"\" href='BookDetail.aspx?id=" + model.Book.Id + "' target=\"_blank\" >" + model.Book.Title + "</a> </td>"); builder.Append("<td><span class=\"price\">¥" + model.Book.UnitPrice.ToString("0.00") + "</span></td>"); builder.Append("<td>" + model.Count + "</td> </tr>"); totleMoney = totleMoney + (model.Count * model.Book.UnitPrice); } strHtml = builder.ToString(); } }
/// <summary> /// 向购物车中添加商品信息。 /// </summary> protected void AddCart() { if (!string.IsNullOrEmpty(Request.QueryString["id"])) { int bookId = 0; if (!int.TryParse(Request.QueryString["id"], out bookId)) { Response.Redirect("/showinfo.aspx?msg=" + Server.UrlEncode("参数错误") + "&url=BookList.aspx" + "&txt=" + Server.UrlEncode("返回图书列表")); } else { BLL.BookManager bll = new BookShop.BLL.BookManager(); Model.Book model = bll.GetModel(bookId);//根据传递过来的书的编号查找该书. if (model != null) { BLL.CartManager cartBll = new BookShop.BLL.CartManager(); int userId = ((Model.User)Session["user"]).Id; //得到了当前登录用户的编号. Model.Cart cartModel = cartBll.GetModel(model.Id, userId); //根据用户的编号,与书的编号,找出购物车中的商品项. if (cartModel == null) //如果该条件成立,向购物车表中插入一条记录 { Model.Cart ModelCart = new BookShop.Model.Cart(); ModelCart.User = (Model.User)Session["user"]; ModelCart.Book = model; ModelCart.Count = 1; cartBll.Add(ModelCart); } else//更新该商品项的数量 { cartModel.Count = cartModel.Count + 1; cartBll.Update(cartModel); } } else { Response.Redirect("/showinfo.aspx?msg=" + Server.UrlEncode("该书不存在") + "&url=BookList.aspx" + "&txt=" + Server.UrlEncode("返回图书列表")); } } } }
public void ProcessRequest(HttpContext context) { if (!string.IsNullOrEmpty(context.Request.Form["action"])) { string action = context.Request.Form["action"]; if (action == "change")//更新商品数量 { int pk, bookId, count; if (!int.TryParse(context.Request.Form["pk"], out pk)) { context.Response.Write("no-参数错误!"); return; } if (!int.TryParse(context.Request.Form["count"], out count)) { context.Response.Write("no-参数错误!"); return; } if (!int.TryParse(context.Request.Form["bookId"], out bookId)) { context.Response.Write("no-参数错误!"); return; } BLL.BookManager bookBll = new BookShop.BLL.BookManager(); Model.Book modelBook = bookBll.GetModel(bookId); if (modelBook != null)//看一下该书是否存在 { BLL.CartManager cartBll = new BookShop.BLL.CartManager(); Model.Cart cartModel = cartBll.GetModel(pk); if (cartModel != null)//根据主键查找该购物车中商品项 { cartModel.Count = count; cartBll.Update(cartModel);//完成数量的更新 context.Response.Write("yes"); } else { context.Response.Write("no"); return; } } else { context.Response.Write("no"); } } //删除一条记录 else if (action == "delete") { if (!string.IsNullOrEmpty(context.Request.Form["pk"])) { int pkId = 0; if (!int.TryParse(context.Request.Form["pk"], out pkId)) { context.Response.Write("no"); return; } BLL.CartManager bllCart = new BookShop.BLL.CartManager(); bllCart.Delete(pkId); context.Response.Write("yes"); } } } }
/// <summary> /// 取出购物车中的商品项. /// </summary> protected void BindRepeater() { BLL.CartManager bll = new BookShop.BLL.CartManager(); this.RepeaterCart.DataSource = bll.GetModelList("UserId=" + ((Model.User)Session["user"]).Id);//用户只能查看自己放在购物车中的商品项. this.RepeaterCart.DataBind(); }