public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; if (!string.IsNullOrEmpty(context.Request.QueryString["term"])) { string term = context.Request.QueryString["term"]; //接收发过来的需要搜索的数据. BLL.BookManager bll = new BookShop.BLL.BookManager(); List <Model.Book> bookList = bll.GetLikeList(term); //获取相应的图书 List <string> list = new List <string>(); foreach (Model.Book model in bookList) { list.Add(model.Title);//将书名放在一个新的集合中. } System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer(); string str = js.Serialize(list.ToArray()); context.Response.Write(str);//将list集合中保存的书名转成数组,并且声称Json格式返回. } }
/// <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("返回图书列表")); } } } }
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>¥" + 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"); } }
/// <summary> /// 绑定Repeater图书列表 /// </summary> protected void BindRepeaterBookList(int page) { int categoryId = 0; //类别编号 int pageCount = 0; //总页数 //接收类别编号 if (!int.TryParse(Request.QueryString["category"], out categoryId)) { categoryId = 0; } BLL.BookManager bll = new BookShop.BLL.BookManager(); pageCount = bll.GetPageCount(10, categoryId);//获取总页数 if (page <= 1) { page = 1; } if (page > pageCount) { page = pageCount; } currentPage = page.ToString();//将当前页码值给currentPage,然后在前台隐藏域中获取该变量的值 // this.RepeaterList.DataSource = bll.GetModelList(""); string order = string.Empty; if (ViewState["orderby"] != null) { order = ViewState["orderby"].ToString(); } // this.RepeaterList.DataSource = bll.GetPageList(page, 10, categoryId);//获取到了分页的数据 this.RepeaterList.DataSource = bll.GetPageList(page, 5, categoryId, order);//根据传递过来的页码值,获取当前页码对应的数据。 this.lblPageCount.Text = pageCount.ToString(); this.RepeaterList.DataBind(); this.lblCurrentPage.Text = page.ToString(); }
protected void Page_Load(object sender, EventArgs e) { BLL.BookManager bll = new BookShop.BLL.BookManager(); this.rssRepeater.DataSource = bll.GetModelList(""); this.rssRepeater.DataBind(); }
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> /// 绑定Repeater图书列表 /// </summary> protected void BindRepeaterBookList() { int categoryId = 0; //类别编号 int pageCount = 0; //总页数 int page = 1; //表示当前页 //接收类别编号 if (!int.TryParse(Request.QueryString["category"], out categoryId)) { categoryId = 0; } //接收URL传递过来的当前页码值. if (!int.TryParse(Request.QueryString["page"], out page)) { page = 1; } BLL.BookManager bll = new BookShop.BLL.BookManager(); pageCount = bll.GetPageCount(5, categoryId);//获取总页数 if (page <= 1) { page = 1; } if (page > pageCount) { page = pageCount; } this.PageData2.PageCount = pageCount; this.PageData2.CurrentPage = page; // this.RepeaterList.DataSource = bll.GetModelList(""); string order = string.Empty; //接收排序依据 if (!string.IsNullOrEmpty(Request.QueryString["orderby"])) { if (Request.QueryString["orderby"] == "PublishDate asc") { this.PublishData.Text = "出版日期↓"; //ViewState["orderby"] = "PublishDate asc"; } else if (Request.QueryString["orderby"] == "PublishDate desc") { this.PublishData.Text = "出版日期↑"; // ViewState["orderby"] = "PublishDate desc"; } order = Request.QueryString["orderby"]; } if (ViewState["orderby"] != null) { order = ViewState["orderby"].ToString(); } // this.RepeaterList.DataSource = bll.GetPageList(page, 10, categoryId);//获取到了分页的数据 this.RepeaterList.DataSource = bll.GetPageList(page, 5, categoryId, order); this.RepeaterList.DataBind(); }