购物车2protected void Page_Load(object sender, EventArgs e)
??? { ??????? if (!Page.IsPostBack) ??????? { ??????????? SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbcon"].ToString()); ??????????? conn.Open(); ??????????? SqlCommand cmd = new SqlCommand("select * from sales where sortid='" + Request.QueryString["id"].ToString() + "'", conn); ??????????? SqlDataReader red = cmd.ExecuteReader(); ??????????? if (red.Read()) ??????????? { ??????????????? Image1.ImageUrl = "pic/" + red["picture"].ToString()+".jpg"; ??????????????? Label51.Text = red["name"].ToString(); ??????????????? Label53.Text = red["price"].ToString(); ??????????????? Label55.Text = red["sales"].ToString(); ??????????????? Label8.Text = red["inventory"].ToString(); ??????????? } ??????????? conn.Close(); ??????? } ??? } ??? protected void ImageButton3_Click(object sender, ImageClickEventArgs e) ??? { ??????? if (Session["id"] != null)//如果用户没有登录 ??????? { ??????????? if (Session["Cart"] == null)//如果购物篮不存在 ??????????? { ??????????????? this.BuildCart();//创建购物篮并将商品存入 ??????????? } ??????????? else//购物篮存在 ??????????? { ??????????????? DataTable cart = Session["Cart"] as DataTable; ??????????????? if (this.ExistBook(cart))//如果购物篮已存在该商品 ??????????????? { ??????????????????? this.BuildSession(cart); //修改购物篮中的商品 ??????????????? } ??????????? } ??????????? Response.Redirect("Cart.aspx");//跳转到购物车界面 ??????? } ??????? else ??????? {//如果没有登录,跳转到登录界面 ??????????? Response.Redirect("login.aspx"); ??????? } ??? } ??? public bool ExistBook(DataTable cart) ??? { ??????? foreach (DataRow dr in cart.Rows) ??????? { ??????????? if (dr["sortid"].ToString().Equals(Request.QueryString["id"].ToString())) ??????????? { ??????????????? dr["Number"] = Convert.ToInt32(dr["Number"]) + 1; ??????????????? Session["Cart"] = cart; ??????????????? Response.Redirect("Cart.aspx"); ??????????? } ??????? } ??????? return true; ??? } ??? /// <summary> ??? /// 新建购物车表 ??? /// </summary> ??? public void BuildCart() ??? {//创建购物车 ??????? DataTable cart = new DataTable();//已经创建了表,但是没有字段 ??????? cart.Columns.Add("sortid"); ??????? cart.Columns.Add("sortname"); ??????? cart.Columns.Add("Number"); ??????? cart.Columns.Add("price"); ??????? cart.Columns.Add("picture");//cart表中已有5个字段 ??????? //将点击的商品加入购物车中 ??????? this.BuildSession(cart);//往cart中添加一条记录 ??? } ??? /// <summary> ??? /// 添加新书 ??? /// </summary> ??? /// <param name="cart"></param> ??? public void BuildSession(DataTable cart) ??? { ??????? //新建一个数据行的记录 ??????? DataRow dr = cart.NewRow(); ??????? dr["sortid"] = Request.QueryString["id"].ToString();//存商品编号 ??????? dr["sortname"] = this.Label51.Text.Trim();//存商品名称 ??????? dr["Number"] = "1";//存商品数量 ??????? dr["price"] = this.Label53.Text.Trim();//存入单价 ??????? dr["picture"] = this.Image1.ImageUrl.Trim();//存入商品图片地址 ??????? cart.Rows.Add(dr);//将数据行加入到cart表中 ??????? //if (用户已登录) {
??????? //??? if (购物篮是否为空 == false) { ??????? //?????? 将购物篮中的数据显示到GridView ??????? //??? } ??????? //} ??? } ??? /// <summary> ??? /// 结算 ??? /// </summary> ??? /// <param name="sender"></param> ??? /// <param name="e"></param> ??? protected void imgb_Salary_Click(object sender, ImageClickEventArgs e) ??? { ??? //??? if (Session["Cart"] == null || ((DataTable)Session["Cart"]).Rows.Count == 0) ??? //??? { ??? //??????? Response.Write("<script>alert('您的购物车为空,请先将图书放入购物车!');document.location='BookList.aspx';</script>"); ??? //??????? return; ??? //??? } ??? //??? Order order = new Order(); ??? //??? order.OrderDate = DateTime.Now; ??? //??? if (this.ltrSalary.Text != String.Empty) ??? //??? { ??? //??????? order.TotalPrice = Convert.ToDecimal(this.ltrSalary.Text); ??? //??? } ??? //??? order.User = user; ??? //??? order = OrderManager.AddOrder(order); ??? //??? OrderBook orderbook = new OrderBook(); ??? //??? Book book = new Book(); ??? //??? Order orders = new Order(); ??? //??? foreach (DataRow dr in ((DataTable)Session["Cart"]).Rows) ??? //??? { ??? //??????? book.Id = Convert.ToInt32(dr["BookId"]); ??? //??????? orders.Id = order.Id; ??? //??????? orderbook.Book = book; ??? //??????? orderbook.Order = orders; ??? //??????? orderbook.Quantity = Convert.ToInt32(dr["Number"]); ??? //??????? orderbook.UnitPrice = Convert.ToDecimal(dr["UnitPrice"]); ??? /// <summary>
??? /// 绑定GridView方法 ??? /// </summary> ??? private void BindGridView() ??? { ??????? DataTable cart = Session["cart"] as DataTable; ??????? TotalPrice(cart); ??????? this.gvCart.DataSource = Session["cart"] as DataTable;//将session购物篮中的数据显示出来 ??????? this.gvCart.DataBind(); ??? } ??? /// <summary>
??? /// GridView编辑按钮处理事件 ??? /// </summary> ??? /// <param name="sender"></param> ??? /// <param name="e"></param> ??? protected void gvCart_RowEditing(object sender, GridViewEditEventArgs e) ??? { ??????? gvCart.EditIndex = e.NewEditIndex; ??????? BindGridView(); ??? } ??? /// <summary>
??? /// GridView分页处理事件 ??? /// </summary> ??? /// <param name="sender"></param> ??? /// <param name="e"></param> ??? protected void gvCart_PageIndexChanging(object sender, GridViewPageEventArgs e) ??? { ??????? gvCart.PageIndex = e.NewPageIndex; ??????? BindGridView(); ??? } ??? /// <summary>
??? /// GridView删除按钮处理事件 ??? /// </summary> ??? /// <param name="sender"></param> ??? /// <param name="e"></param> ??? protected void gvCart_RowDeleting(object sender, GridViewDeleteEventArgs e) ??? { ??????? DataTable cart = Session["cart"] as DataTable; ??????? cart.Rows[e.RowIndex].Delete(); ??????? Session["cart"] = cart; ??????? BindGridView(); ??? } ??? /// <summary>
??? /// GridView取消按钮处理事件 ??? /// </summary> ??? /// <param name="sender"></param> ??? /// <param name="e"></param> ??? protected void gvCart_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) ??? { ??????? gvCart.EditIndex = -1; ??????? BindGridView(); ??? } ??? /// <summary>
??? /// GridView更新按钮处理事件 ??? /// </summary> ??? /// <param name="sender"></param> ??? /// <param name="e"></param> ??? protected void gvCart_RowUpdating(object sender, GridViewUpdateEventArgs e) ??? { ??????? DataTable cart = Session["cart"] as DataTable; ??????? foreach (DataRow dr in cart.Rows) ??????? { ??????????? if (dr["sortname"].ToString().Equals((gvCart.Rows[e.RowIndex].FindControl("lblBookName") as Label).Text)) ??????????? { ??????????????? dr["Number"] = (gvCart.Rows[e.RowIndex].FindControl("txtNumber") as TextBox).Text.Trim(); ??????????? } ??????? } ??? /// <summary>
??? /// GridView数据绑定后激发的事件 ??? /// </summary> ??? /// <param name="sender"></param> ??? /// <param name="e"></param> ??? protected void gvCart_RowDataBound(object sender, GridViewRowEventArgs e) ??? { ??????? if (e.Row.RowType == DataControlRowType.DataRow) ??????? { ??????????? LinkButton lb = e.Row.FindControl("lnkbtnDelete") as LinkButton; ??????????? lb.Attributes.Add("onclick", "return confirm('确定删除吗?')"); ??????? } ??? } |