protected void ButtonConfirm_Click(object sender, EventArgs e) { //删除所有购物车明细,创建订单,创建订单明细。 //一个字符串对象,保存用户名 String userName = ((Site1)Page.Master).CurrentUserName; if (userName.Equals("")) return; if(TextBox1.Text.Trim().Length == 0 || TextBox2.Text.Trim().Length == 0 || TextBox3.Text.Trim().Length == 0 || TextBox4.Text.Trim().Length == 0) { Response.Write("<script>alert('请输入所有资料');</script>"); } String ReciverName = TextBox1.Text.Trim(); String Address = TextBox2.Text.Trim(); String Phone = TextBox3.Text.Trim(); String PostCode = TextBox4.Text.Trim(); using (ToyStoreEntities context = new ToyStoreEntities()) { Order myOrder = new Order(); myOrder.ReciverName = ReciverName; myOrder.Address = Address; myOrder.Phone = Phone; myOrder.PostCode = PostCode; myOrder.CreateTime = DateTime.Now; myOrder.PayTime = DateTime.Now; myOrder.UserName = ((Site1)Page.Master).CurrentUserName; context.AddToOrders(myOrder); context.SaveChanges(); var cartDetails = from p in context.CartDetails where p.Cart.UserName == ((Site1)Page.Master).CurrentUserName select p; foreach (CartDetail cartDetail in cartDetails) { OrderDetail orderDetail = new OrderDetail() { OrderId = myOrder.Id, ProductId = cartDetail.ProductId, Qty = cartDetail.OTY, UnitPrice = cartDetail.Product.Price }; myOrder.OrderDetails.Add(orderDetail); context.CartDetails.DeleteObject(cartDetail); orderDetail.Product.StockQty -= cartDetail.OTY; } context.SaveChanges(); //新建一个OrderInfo类,即创建一个订单 Response.Redirect(String.Format("~/ViewOrderDetail.aspx?ID={0}", myOrder.Id)); } }
protected void Button1_Click(object sender, EventArgs e) { using (ToyStoreEntities context = new ToyStoreEntities()) { Product product = new Product(); product.Name = TextBox1.Text.Trim(); product.Price = Convert.ToDecimal(TextBox2.Text.Trim()); product.MarketPrice = Convert.ToDecimal(TextBox3.Text.Trim()); product.StockQty = Convert.ToInt32(TextBox4.Text.Trim()); product.Description = TextBox5.Text; product.ProductTypeId = Convert.ToInt32(DropDownList1.SelectedValue); product.BrandId = Convert.ToInt32(DropDownList2.SelectedValue); product.MaterialId = Convert.ToInt32(DropDownList3.SelectedValue); product.SuitableAgeId = Convert.ToInt32(DropDownList4.SelectedValue); if (FileUpload1.HasFile) { product.PicFileName = FileUpload1.FileName; //将文件上传到Upload目录下 FileUpload1.SaveAs(Server.MapPath(".\\Pic\\" + product.PicFileName)); } context.AddToProducts(product); context.SaveChanges(); Response.Redirect("~/ViewProduct.aspx?id=" + product.Id); } }
protected void Button2_Click(object sender, EventArgs e) { using (ToyStoreEntities context = new ToyStoreEntities()) { //查询出该订单 var order = (from orderInfo in context.Orders where orderInfo.Id == CurrentOrder.Id select orderInfo).FirstOrDefault(); order.ReciveTime = DateTime.Now; context.SaveChanges(); Response.Redirect(Request.Url.ToString()); } }
protected void Button1_Click(object sender, EventArgs e) { if (TextBox1.Text.Length == 0 || TextBox2.Text.Length == 0 || TextBox3.Text.Length == 0 || TextBox4.Text.Length == 0 || TextBox2.Text != TextBox3.Text) { Response.Write("<script> alert( '注册失败') </script> "); return; } else { //检查用户名是否存在 using (ToyStoreEntities context = new ToyStoreEntities()) { var query = from userInfo in context.Users where userInfo.UserName == TextBox1.Text select userInfo; if (query.Count() != 0) { Response.Write("<script> alert( '注册失败') </script> "); return; } else { User newUser = new User(); newUser.UserName = TextBox1.Text; newUser.Password = TextBox2.Text; newUser.PhoneNumber = TextBox3.Text; if (Request.QueryString["Admin"] == "1") { newUser.IsAdmin = UserType.Admin; } else { newUser.IsAdmin = UserType.RegUser; } context.AddToUsers(newUser); context.SaveChanges(); Response.Write("<script> alert( '注册成功');window.location.href = 'Default.aspx';</script>"); } } } }
protected void DeleteButton_Click(object sender, EventArgs e) { using (ToyStoreEntities context = new ToyStoreEntities()) { var query = from p in context.Products where p.Id == CurrentProduct.Id select p; context.Products.DeleteObject(query.FirstOrDefault()); context.SaveChanges(); } Response.Redirect("~"); }
protected void AddToCartButton_Click(object sender, EventArgs e) { using (ToyStoreEntities context = new ToyStoreEntities()) { String CurrentUserName = ((Site1)Page.Master).CurrentUserName; Cart myCart; //找到该用户的购物车 var query = from p in context.Carts where p.UserName == CurrentUserName select p; if (query.Count() == 0) { myCart = new Cart(); myCart.UserName = CurrentUserName; context.AddToCarts(myCart); } else { myCart = query.First(); } //查找是否已有该购物车明细 var query1 = from p in myCart.CartDetails where p.Product.Id == CurrentProduct.Id select p; CartDetail myCartDetail; if (query1.Count() == 0) { //如果没有,新增 myCartDetail = new CartDetail(); myCartDetail.Cart = myCart; myCartDetail.ProductId = CurrentProduct.Id; myCartDetail.OTY = 1; } else { //如果有,修改数量 myCartDetail = query1.First(); myCartDetail.OTY += 1; } context.SaveChanges(); } }
protected void ResetQuantity() { String userName = ((Site1)Page.Master).CurrentUserName; if (userName.Equals("")) return; using (ToyStoreEntities context = new ToyStoreEntities()) { //对于表格中的每一行 foreach (GridViewRow row in GridView1.Rows) { //获取表格中填的数量 int quantity = Convert.ToInt32(((TextBox)row.FindControl("QuantityTextbox")).Text); //获取软件名称 string softwareName = row.Cells[0].Text; //找到该购物车明细 var query = from cartDetail in context.CartDetails where cartDetail.Product.Name == softwareName && cartDetail.Cart.User.UserName == userName select cartDetail; if (quantity < 0) { //如果数量小于0,删除该购物车明细 context.CartDetails.DeleteObject(query.FirstOrDefault()); } else { //修改数量 query.FirstOrDefault().OTY = quantity; } } //保存数据库 context.SaveChanges(); } }