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 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); } } }
/// <summary> /// Deprecated Method for adding a new object to the Orders EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToOrders(Order order) { base.AddObject("Orders", order); }
/// <summary> /// Create a new Order object. /// </summary> /// <param name="id">Initial value of the Id property.</param> /// <param name="reciverName">Initial value of the ReciverName property.</param> /// <param name="address">Initial value of the Address property.</param> /// <param name="phone">Initial value of the Phone property.</param> /// <param name="postCode">Initial value of the PostCode property.</param> /// <param name="createTime">Initial value of the CreateTime property.</param> public static Order CreateOrder(global::System.Int32 id, global::System.String reciverName, global::System.String address, global::System.String phone, global::System.String postCode, global::System.DateTime createTime) { Order order = new Order(); order.Id = id; order.ReciverName = reciverName; order.Address = address; order.Phone = phone; order.PostCode = postCode; order.CreateTime = createTime; return order; }