Пример #1
0
 /// <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("返回图书列表"));
             }
         }
     }
 }
Пример #2
0
 protected void Page_Load(object sender, EventArgs e)
 {
     if (!string.IsNullOrEmpty(Request.QueryString["id"]))
     {
         int id = 0;
         if (!int.TryParse(Request.QueryString["id"], out id))
         {
             Response.Redirect("BookList2.aspx");
         }
         else
         {
             BLL.BookManager bll   = new BookShop.BLL.BookManager();
             Model.Book      model = bll.GetModel(id);//根据书的编号,查找该书.
             if (model != null)
             {
                 StringBuilder builder = new StringBuilder();
                 builder.Append("<table border='1' >");
                 builder.Append("<tr><th width='30px'>书名:</th><td>" + model.Title + "</td></tr>");
                 builder.Append("<tr><th>封面:</th><td><img src=/Images/BookCovers/" + model.ISBN + ".jpg/></td></tr>");
                 builder.Append("<tr><th>作者:</th><td>" + model.Author + "</td></tr>");
                 builder.Append("<tr><th>出版日期::</th><td>" + model.PublishDate.ToShortDateString() + "</td></tr>");
                 builder.Append("<tr><th>单价:</th><td>&yen;" + model.UnitPrice.ToString("0.00") + "<a href='cart.aspx?id=" + model.Id + "'><img src='/Images/sale.gif' border='0'/></a></td></tr>");
                 builder.Append("<tr><th>简介:</th><td>" + model.ContentDescription + "</td></tr>");
                 builder.Append("<tr><th>目录::</th><td>" + model.TOC + "</td></tr>");
                 builder.Append("</table>");
                 strHtml = builder.ToString();
             }
             else
             {
                 Response.Redirect("/showinfo.aspx?msg=" + Server.UrlEncode("图书部存在") + "&url=/BookList2.aspx" + "&txt=" + Server.UrlEncode("图书列表页面"));
             }
         }
     }
     else
     {
         Response.Redirect("BookList2.aspx");
     }
 }
Пример #3
0
 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");
             }
         }
     }
 }