protected void Page_Load(object sender, EventArgs e) { //加载页面时 if (!IsPostBack) { //如果是第一次加载 String userName = ((Site1)Page.Master).CurrentUserName; if (userName.Equals("")) return; using (LiBoClothesShopEntities context = new LiBoClothesShopEntities()) { //查询出该用户的所有订单 var query = from orderInfo in context.OrderInfoes select new { Id = orderInfo.ID, UserName = orderInfo.UserName, OrderTime = orderInfo.CreateTime, Total = (from orderDetail in orderInfo.OrderDetails select (decimal?)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 Page_Load(object sender, EventArgs e) { //加载页面时 if (!IsPostBack) { //如果是第一次加载 String userName = ((Site1)Page.Master).CurrentUserName; if (userName.Equals("")) return; using (LiBoClothesShopEntities context = new LiBoClothesShopEntities()) { //查询出该用户的所有购物车明细 var query = from cartDetail in context.CartDetails where cartDetail.CartInfo.UserName == userName select new { Name = cartDetail.Cloth.Name, UnitPrice = cartDetail.Cloth.Price, Quantity = cartDetail.QTY, Subtotal = cartDetail.Cloth.Price * cartDetail.QTY }; //GridView1的数据源为查询结果 GridView1.DataSource = query; //绑定数据源 GridView1.DataBind(); } } }
protected void Button1_Click(object sender, EventArgs e) { using (LiBoClothesShopEntities context = new LiBoClothesShopEntities()) { //对于表格中的每一行 foreach (GridViewRow row in GridView1.Rows) { int id = Convert.ToInt32(row.Cells[0].Text); ColthColor color = (from p in context.ColthColors where p.Id == id select p).FirstOrDefault(); FileUpload upload = (FileUpload)row.FindControl("imgupload"); if (upload.HasFile) { color.PicFileName = upload.FileName; //将文件上传到Upload目录下 upload.SaveAs(Server.MapPath(".\\Upload\\" + color.PicFileName)); } } context.SaveChanges(); Response.Redirect("~/ViewCloth.aspx?id=" + ProductId); } }
protected void AddToCartButton_Click(object sender, EventArgs e) { using (LiBoClothesShopEntities context = new LiBoClothesShopEntities()) { String CurrentUserName = ((Site1)Page.Master).CurrentUserName; int colorId = Convert.ToInt32(DropDownListColor.SelectedItem.Value); int sizeId = Convert.ToInt32(DropDownListSize.SelectedValue); //查找用户是否有购物车 var query = from cart in context.CartInfoes where cart.UserName == CurrentUserName select cart; CartInfo myCart; if (query.Count() == 0) { myCart = new CartInfo() { UserName = CurrentUserName, TotalPrice = 0 }; context.AddToCartInfoes(myCart); context.SaveChanges(); } else { myCart = query.FirstOrDefault(); } //查找是否已有该购物车明细 var query1 = from p in context.CartDetails where p.CartInfo.UserName == CurrentUserName && p.ColorId == colorId && p.SizeId == sizeId && p.ClothId == CurrentProduct.Id select p; CartDetail myCartDetail; if (query1.Count() == 0) { //如果没有,新增 myCartDetail = new CartDetail(); myCartDetail.CartId = myCart.ID; myCartDetail.SizeId = sizeId; myCartDetail.ColorId = colorId; myCartDetail.ClothId = CurrentProduct.Id; myCartDetail.QTY = 1; context.AddToCartDetails(myCartDetail); } else { //如果有,修改数量 myCartDetail = query1.First(); myCartDetail.QTY += 1; } //修改总价 myCart.TotalPrice += CurrentProduct.Price; context.SaveChanges(); } }
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 (LiBoClothesShopEntities context = new LiBoClothesShopEntities()) { OrderInfo myOrder = new OrderInfo(); myOrder.Consignee = ReciverName; myOrder.Address = Address; myOrder.Phone = Phone; myOrder.Zip = PostCode; myOrder.CreateTime = DateTime.Now; myOrder.PaymentTime = DateTime.Now; myOrder.SendTime = null; myOrder.ReceiptTime = null; myOrder.UserName = ((Site1)Page.Master).CurrentUserName; context.AddToOrderInfoes(myOrder); context.SaveChanges(); var cartDetails = from p in context.CartDetails where p.CartInfo.UserName == ((Site1)Page.Master).CurrentUserName select p; foreach (CartDetail cartDetail in cartDetails) { OrderDetail orderDetail = new OrderDetail() { OrderId = myOrder.ID, ClothesId = cartDetail.ClothId.Value, QTY = cartDetail.QTY, UnitPrice = cartDetail.Cloth.Price, SizeId = cartDetail.SizeId.Value, ColorId = cartDetail.ColorId.Value }; myOrder.OrderDetails.Add(orderDetail); context.CartDetails.DeleteObject(cartDetail); } context.SaveChanges(); //新建一个OrderInfo类,即创建一个订单 Response.Redirect(String.Format("~/ViewOrderDetail.aspx?ID={0}", myOrder.ID)); } }
protected void Button2_Click(object sender, EventArgs e) { using (LiBoClothesShopEntities context = new LiBoClothesShopEntities()) { //查询出该订单 var order = (from orderInfo in context.OrderInfoes where orderInfo.ID == CurrentOrder.ID select orderInfo).FirstOrDefault(); order.ReceiptTime = DateTime.Now; context.SaveChanges(); Response.Redirect("~/Default.aspx"); } }
protected void Button1_Click(object sender, EventArgs e) { String name = TextBox1.Text.Trim(); using (LiBoClothesShopEntities context = new LiBoClothesShopEntities()) { GenderInfo newObj = new GenderInfo() { Name = name }; context.AddToGenderInfoes(newObj); context.SaveChanges(); Response.Redirect("~/Default.aspx"); } }
protected void Button1_Click(object sender, EventArgs e) { String name = TextBox1.Text.Trim(); decimal price = Convert.ToDecimal(TextBox2.Text.Trim()); decimal marketprice = Convert.ToDecimal(TextBox3.Text.Trim()); int qty = Convert.ToInt32(TextBox4.Text.Trim()); int brandId = Convert.ToInt32(DropDownList1.SelectedValue); int genderId = Convert.ToInt32(DropDownList2.SelectedValue); int id; using (LiBoClothesShopEntities context = new LiBoClothesShopEntities()) { Cloth cloth = new Cloth(); cloth.Name = name; cloth.Price = price; cloth.OriginalPrice = marketprice; cloth.Qty = qty; cloth.BrandId = brandId; cloth.GenderId = genderId; context.AddToClothes(cloth); context.SaveChanges(); id = cloth.Id; foreach (ListItem item in CheckBoxList1.Items) { if (item.Selected) { ClothSzie size = new ClothSzie(); size.Cloth = cloth; size.SizeId = Convert.ToInt32(item.Value); context.AddToClothSzies(size); } } context.SaveChanges(); foreach (ListItem item in CheckBoxList2.Items) { if (item.Selected) { ColthColor color = new ColthColor(); color.Cloth = cloth; color.ColorId = Convert.ToInt32(item.Value); context.AddToColthColors(color); } } context.SaveChanges(); } Response.Redirect(String.Format("~/AddClothStep2.aspx?id={0}", id)); }
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 (LiBoClothesShopEntities context = new LiBoClothesShopEntities()) { var query = from userInfo in context.UserInfoes where userInfo.UserName == TextBox1.Text select userInfo; if (query.Count() != 0) { Response.Write("<script> alert( '注册失败') </script> "); return; } else { UserInfo newUser = new UserInfo(); newUser.UserName = TextBox1.Text; newUser.Password = TextBox2.Text; newUser.Phone = TextBox3.Text; if (Convert.ToInt32(Request.QueryString["Type"]) == 2) { newUser.IsAdmin = true; } else { newUser.IsAdmin = false; } context.AddToUserInfoes(newUser); context.SaveChanges(); Response.Write("<script> alert( '注册成功');window.location.href = 'Default.aspx';</script>"); } } } }
protected void Page_Load(object sender, EventArgs e) { //加载页面时 if (!IsPostBack) { //如果是第一次加载 String userName = ((Site1)Page.Master).CurrentUserName; if (userName.Equals("")) return; using (LiBoClothesShopEntities context = new LiBoClothesShopEntities()) { //查询出该用户的所有购物车明细 var query = from cartDetail in context.CartDetails where cartDetail.CartInfo.UserName == userName select new { Name = cartDetail.Cloth.Name, UnitPrice = cartDetail.Cloth.Price, Quantity = cartDetail.QTY, Subtotal = cartDetail.Cloth.Price * cartDetail.QTY, Size = cartDetail.SizeInfo.Name, Color = cartDetail.ColorInfo.Name }; if (query.Count() == 0) { //如果购物车是空的 //提示 MultiView1.SetActiveView(EmptyView); } else { //显示购物车信息 MultiView1.SetActiveView(NonEmptyView); //GridView1的数据源为查询结果 GridView1.DataSource = query; //绑定数据源 GridView1.DataBind(); //显示总价 Label1.Text = ((Site1)Page.Master).PriceFormater((from p in context.CartInfoes where p.UserName == userName select p).FirstOrDefault().TotalPrice); } } } }
protected void ResetQuantity() { String userName = ((Site1)Page.Master).CurrentUserName; if (userName.Equals("")) return; using (LiBoClothesShopEntities context = new LiBoClothesShopEntities()) { decimal totalPrice = 0; //对于表格中的每一行 foreach (GridViewRow row in GridView1.Rows) { //获取表格中填的数量 int quantity = Convert.ToInt32(((TextBox)row.FindControl("QuantityTextbox")).Text); //获取软件名称 string productName = row.Cells[0].Text; string size = row.Cells[1].Text; string color = row.Cells[2].Text; //找到该购物车明细 var query = from cartDetail in context.CartDetails where cartDetail.Cloth.Name == productName && cartDetail.ColorInfo.Name == color && cartDetail.SizeInfo.Name == size select cartDetail; if (quantity <= 0) { //如果数量小于0,删除该购物车明细 context.CartDetails.DeleteObject(query.FirstOrDefault()); } else { //修改数量 query.FirstOrDefault().QTY = quantity; } totalPrice += query.FirstOrDefault().QTY * query.FirstOrDefault().Cloth.Price; } //修改购物车的总价 (from p in context.CartInfoes where p.UserName == userName select p).FirstOrDefault().TotalPrice = totalPrice; //保存数据库 context.SaveChanges(); } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { using (LiBoClothesShopEntities context = new LiBoClothesShopEntities()) { var query = from p in context.ColthColors where p.ClothId == ProductId select new { ID = p.Id, Name = p.ColorInfo.Name }; GridView1.DataSource = query; GridView1.DataBind(); } } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { using (LiBoClothesShopEntities context = new LiBoClothesShopEntities()) { var q1 = from p in context.Brands select new { ID = p.Id, Name = p.Name }; DropDownList1.DataSource = q1; DropDownList1.DataBind(); var q2 = from p in context.GenderInfoes select new { ID = p.Id, Name = p.Name }; DropDownList2.DataSource = q2; DropDownList2.DataBind(); var q3 = from p in context.SizeInfoes select new { ID = p.Id, Name = p.Name }; CheckBoxList1.DataSource = q3; CheckBoxList1.DataBind(); var q4 = from p in context.ColorInfoes select new { ID = p.Id, Name = p.Name }; CheckBoxList2.DataSource = q4; CheckBoxList2.DataBind(); } } }
protected void ButtonLogin_Click(object sender, EventArgs e) { //登陆按钮按下时 //获取用户名和密码 string userName = TextBoxUserName.Text.Trim(); string password = TextBoxPassword.Text.Trim(); using (LiBoClothesShopEntities context = new LiBoClothesShopEntities()) { //按照用户名和密码查找用户信息 IQueryable<UserInfo> userQuery = from user in context.UserInfoes where user.UserName == userName && user.Password == password select user; if (userQuery.Count() > 0) { //如果能找到该用户 //写入当前用户名和类型 CurrentUserName = userName; CurrentUserType = (userQuery.First().IsAdmin) ? UserType.Admin : UserType.RegUser; ; } else { return; } if (CurrentUserType == UserType.RegUser) { //如果当前用户是注册用户 //网页上方切换到注册用户页面 HeadMultiView.SetActiveView(RegUserView); WelcomeUser.Text = "欢迎您," + CurrentUserName; } else { //否则,当前用户是管理员 //切换到管理员页面 HeadMultiView.SetActiveView(AdminView); WelcomeAdmin.Text = "欢迎您," + CurrentUserName; } } }
protected void Page_Load(object sender, EventArgs e) { using (LiBoClothesShopEntities context = new LiBoClothesShopEntities()) { var query = from p in context.Clothes select new { Id = p.Id, Name = (p.ColthColors.Count == 0 || p.ColthColors.FirstOrDefault().PicFileName == null || p.ColthColors.FirstOrDefault().PicFileName.Length == 0) ? p.Name : @"<table><tr><td><img src='./Upload/" + p.ColthColors.FirstOrDefault().PicFileName + "' height='100px' /></td></tr><tr><td>" + p.Name + "</td></tr></table>" }; GridView1.DataSource = query; GridView1.DataBind(); } }
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 (LiBoClothesShopEntities context = new LiBoClothesShopEntities()) { //获取该商品的信息 var query = from product in context.Clothes where product.Id == ProductId select product; //如果找不到该软件 if (query.Count() == 0) { //显示"找不到" Table1.Visible = false; MessageLabel.Visible = true; MessageLabel.Text = "找不到这个服装"; } else { //否则 //当前商品为查询结果的第一个 CurrentProduct = query.First(); //显示商品的信息 NameLabel.Text = CurrentProduct.Name; PriceLabel.Text = ((Site1)Page.Master).PriceFormater(CurrentProduct.Price); MarketPriceLabel.Text = ((Site1)Page.Master).PriceFormater(CurrentProduct.OriginalPrice); BrandLabel.Text = CurrentProduct.Brand.Name; GenderLabel.Text = CurrentProduct.GenderInfo.Name; var query2 = from p in CurrentProduct.ColthColors select new { ColorId = p.ColorId, ColorName = p.ColorInfo.Name }; DropDownListColor.DataSource = query2; DropDownListColor.DataTextField = "ColorName"; DropDownListColor.DataValueField = "ColorId"; DropDownListColor.DataBind(); var query3 = from p in CurrentProduct.ClothSzies select new { SizeId = p.SizeId, SizeName = p.SizeInfo.Name }; DropDownListSize.DataSource = query3; DropDownListSize.DataTextField = "SizeName"; DropDownListSize.DataValueField = "SizeId"; DropDownListSize.DataBind(); Label1.Text = "<table>"; foreach (ColthColor c in CurrentProduct.ColthColors) { Label1.Text += "<tr><td><img src='./Upload/"; Label1.Text += c.PicFileName; Label1.Text += "' /></td></tr><tr><td>"; Label1.Text += c.ColorInfo.Name; Label1.Text += "</td></tr>"; } Label1.Text += "</table>"; } } } }
protected void DeleteButton_Click(object sender, EventArgs e) { using (LiBoClothesShopEntities context = new LiBoClothesShopEntities()) { var query = from p in context.Clothes where p.Id == CurrentProduct.Id select p; context.Clothes.DeleteObject(query.FirstOrDefault()); context.SaveChanges(); } Response.Redirect("~"); }
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 (LiBoClothesShopEntities context = new LiBoClothesShopEntities()) { //查询出该订单 var order = (from orderInfo in context.OrderInfoes where orderInfo.ID == id select orderInfo).FirstOrDefault(); //查询出该订单的订单明细 var query2 = from orderDetail in order.OrderDetails select new { Id = orderDetail.ClothesId, ProductName = orderDetail.Cloth.Name, Quantity = orderDetail.QTY, Size = orderDetail.SizeInfo.Name, Color = orderDetail.ColorInfo.Name, UnitPrice = orderDetail.UnitPrice, SubTotal = orderDetail.QTY * orderDetail.UnitPrice }; //GridView1的数据源为查询结果 GridView1.DataSource = query2; //绑定数据源 GridView1.DataBind(); CurrentOrder = order; //计算总价 order.TotalPrice = (from p in query2 select p.SubTotal).Sum(); Label1.Text = order.Consignee; Label2.Text = order.Address; Label3.Text = order.Phone; Label4.Text = order.Zip; Label5.Text = order.CreateTime.ToString(); if (order.PaymentTime != null) Label6.Text = order.PaymentTime.ToString(); else Label6.Text = "未付款"; if (order.SendTime != null) Label7.Text = order.SendTime.Value.ToString(); else Label7.Text = "未发货"; if (order.ReceiptTime != null) Label8.Text = order.ReceiptTime.Value.ToString(); else Label8.Text = "未收货"; Label9.Text = ((Site1)Page.Master).PriceFormater(order.TotalPrice.Value); } } }