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 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; } } } }
/// <summary> /// Deprecated Method for adding a new object to the Products EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToProducts(Product product) { base.AddObject("Products", product); }
/// <summary> /// Create a new Product object. /// </summary> /// <param name="id">Initial value of the Id property.</param> /// <param name="name">Initial value of the Name property.</param> /// <param name="price">Initial value of the Price property.</param> /// <param name="stockQty">Initial value of the StockQty property.</param> /// <param name="picFileName">Initial value of the PicFileName property.</param> /// <param name="marketPrice">Initial value of the MarketPrice property.</param> /// <param name="productTypeId">Initial value of the ProductTypeId property.</param> /// <param name="brandId">Initial value of the BrandId property.</param> /// <param name="materialId">Initial value of the MaterialId property.</param> /// <param name="suitableAgeId">Initial value of the SuitableAgeId property.</param> /// <param name="description">Initial value of the Description property.</param> public static Product CreateProduct(global::System.Int32 id, global::System.String name, global::System.Decimal price, global::System.Int32 stockQty, global::System.String picFileName, global::System.Decimal marketPrice, global::System.Int32 productTypeId, global::System.Int32 brandId, global::System.Int32 materialId, global::System.Int32 suitableAgeId, global::System.String description) { Product product = new Product(); product.Id = id; product.Name = name; product.Price = price; product.StockQty = stockQty; product.PicFileName = picFileName; product.MarketPrice = marketPrice; product.ProductTypeId = productTypeId; product.BrandId = brandId; product.MaterialId = materialId; product.SuitableAgeId = suitableAgeId; product.Description = description; return product; }