protected void Page_Load(object sender, EventArgs e) { //加载页面时 if (!IsPostBack) { //如果是第一次加载 String userName = ((Site1)Page.Master).CurrentUserName; if (userName.Equals("")) return; using (ToyStoreEntities context = new ToyStoreEntities()) { //查询出该用户的所有购物车明细 var query = from cartDetail in context.CartDetails where cartDetail.Cart.UserName == userName select new { Name = cartDetail.Product.Name, UnitPrice = cartDetail.Product.Price, Quantity = cartDetail.OTY, Subtotal = cartDetail.Product.Price * cartDetail.OTY }; //GridView1的数据源为查询结果 GridView1.DataSource = query; //绑定数据源 GridView1.DataBind(); decimal totalPrice = 0; foreach (var a in query) { totalPrice += a.Subtotal; } Label1.Text = ((Site1)Page.Master).PriceFormater(totalPrice); } } }
protected void Page_Load(object sender, EventArgs e) { //加载页面时 if (!IsPostBack) { //如果是第一次加载 String userName = ((Site1)Page.Master).CurrentUserName; if (userName.Equals("")) return; using (ToyStoreEntities context = new ToyStoreEntities()) { //查询出该用户的所有订单 var query = from orderInfo in context.Orders select new { Id = orderInfo.Id, UserName = orderInfo.UserName, OrderTime = orderInfo.CreateTime, Total = (from orderDetail in orderInfo.OrderDetails select orderDetail.UnitPrice * orderDetail.Qty).Sum() }; if (((Site1)Page.Master).CurrentUserType == UserType.RegUser) { query = query.Where(orderInfo => orderInfo.UserName.Equals(userName)); } //GridView1的数据源为查询结果 GridView1.DataSource = query; //绑定数据源 GridView1.DataBind(); } } }
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 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 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 Page_Load(object sender, EventArgs e) { if (!IsPostBack) { using (ToyStoreEntities context = new ToyStoreEntities()) { var q1 = from p in context.ProductTypes select new { ID = p.Id, Name = p.Name }; DropDownList1.DataValueField = "ID"; DropDownList1.DataTextField = "Name"; DropDownList1.DataSource = q1; DropDownList1.DataBind(); var q2 = from p in context.Brands select new { ID = p.Id, Name = p.Name }; DropDownList2.DataValueField = "ID"; DropDownList2.DataTextField = "Name"; DropDownList2.DataSource = q2; DropDownList2.DataBind(); var q3 = from p in context.Materials select new { ID = p.Id, Name = p.Name }; DropDownList3.DataValueField = "ID"; DropDownList3.DataTextField = "Name"; DropDownList3.DataSource = q3; DropDownList3.DataBind(); var q4 = from p in context.Ages select new { ID = p.Id, Name = p.Name }; DropDownList4.DataValueField = "ID"; DropDownList4.DataTextField = "Name"; DropDownList4.DataSource = q4; DropDownList4.DataBind(); } } }
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 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 ButtonLogin_Click(object sender, EventArgs e) { //登陆按钮按下时 //获取用户名和密码 string userName = TextBoxUserName.Text.Trim(); string password = TextBoxPassword.Text.Trim(); using (ToyStoreEntities context = new ToyStoreEntities()) { //按照用户名和密码查找用户信息 IQueryable<User> userQuery = from user in context.Users where user.UserName == userName && user.Password == password select user; if (userQuery.Count() > 0) { //如果能找到该用户 //写入当前用户名和类型 CurrentUserName = userName; CurrentUserType = (userQuery.First().IsAdmin); } else { return; } if (CurrentUserType == UserType.RegUser) { //如果当前用户是注册用户 //网页上方切换到注册用户页面 HeadMultiView.SetActiveView(RegUserView); WelcomeUser.Text = "欢迎您," + CurrentUserName; } else { //否则,当前用户是管理员 //切换到管理员页面 HeadMultiView.SetActiveView(AdminView); WelcomeAdmin.Text = "欢迎您," + CurrentUserName; } } }
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(); } }
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 Page_Load(object sender, EventArgs e) { //检查当前用户类型是否为注册用户 if (((Site1)Page.Master).CurrentUserType == UserType.RegUser) { //如果是 //隐藏编辑按钮 ModifyButton.Visible = false; //隐藏删除按钮 DeleteButton.Visible = false; } else if (((Site1)Page.Master).CurrentUserType == UserType.Admin) { //否则,显示"添加到购物车"按钮 AddToCartButton.Visible = false; } else { //隐藏编辑按钮 ModifyButton.Visible = false; //隐藏删除按钮 DeleteButton.Visible = false; AddToCartButton.Visible = false; } if (!IsPostBack) { using (ToyStoreEntities context = new ToyStoreEntities()) { //获取该商品的信息 var query = from product in context.Products where product.Id == ProductId select product; //如果找不到该软件 if (query.Count() == 0) { //显示"找不到" Table1.Visible = false; MessageLabel.Visible = true; MessageLabel.Text = "找不到这个玩具"; } else { //否则 //当前商品为查询结果的第一个 CurrentProduct = query.First(); //显示商品的信息 Image1.ImageUrl = "~/Pic/" + CurrentProduct.PicFileName; NameLabel.Text = CurrentProduct.Name; PriceLabel.Text = ((Site1)Page.Master).PriceFormater(CurrentProduct.Price); MarketPriceLabel.Text = ((Site1)Page.Master).PriceFormater(CurrentProduct.MarketPrice); IntroLabel.Text = CurrentProduct.Description; ProductTypeLabel.Text = CurrentProduct.ProductType.Name; BrandLabel.Text = CurrentProduct.Brand.Name; AgeLabel.Text = CurrentProduct.Age.Name; MaterialLabel.Text = CurrentProduct.Material.Name; } } } }
protected void Page_Load(object sender, EventArgs e) { //加载页面时 int userType = ((Site1)Page.Master).CurrentUserType; if (userType == UserType.Admin) { Button1.Visible = true; Button2.Visible = false; } else if (userType == UserType.RegUser) { Button1.Visible = false; Button2.Visible = true; } else { Button1.Visible = false; Button2.Visible = false; } if (!IsPostBack) { //如果是第一次加载 String userName = ((Site1)Page.Master).CurrentUserName; if (userName.Equals("")) return; //从url中读取订单号 int id = Convert.ToInt32(Request.QueryString["id"]); using (ToyStoreEntities context = new ToyStoreEntities()) { //查询出该订单 var order = (from orderInfo in context.Orders where orderInfo.Id == id select orderInfo).FirstOrDefault(); //查询出该订单的订单明细 var query2 = from orderDetail in order.OrderDetails select new { Id = orderDetail.ProductId, ProductName = orderDetail.Product.Name, Quantity = orderDetail.Qty, UnitPrice = orderDetail.UnitPrice, SubTotal = orderDetail.Qty * orderDetail.UnitPrice }; //GridView1的数据源为查询结果 GridView1.DataSource = query2; //绑定数据源 GridView1.DataBind(); CurrentOrder = order; Label1.Text = order.ReciverName; Label2.Text = order.Address; Label3.Text = order.Phone; Label4.Text = order.PostCode; Label5.Text = order.CreateTime.ToString(); if (order.PayTime != null) Label6.Text = order.PayTime.Value.ToString(); else Label6.Text = "未付款"; if (order.SendTime != null) Label7.Text = order.SendTime.Value.ToString(); else Label7.Text = "未发货"; if (order.ReciveTime != null) Label8.Text = order.ReciveTime.Value.ToString(); else Label8.Text = "未收货"; decimal totalPrice = 0; foreach (var a in query2) { totalPrice += a.SubTotal; } Label9.Text = ((Site1)Page.Master).PriceFormater(totalPrice); } } }