- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GoodsManage.aspx.cs" Inherits="GoodsManage"
- EnableEventValidation="false" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>商品管理</title>
- <link href="css/Style.css" rel="stylesheet" type="text/css" />
- <script language="javascript" type="text/javascript" src="My97DatePicker/WdatePicker.js"></script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div style="padding: 0px; margin: 0px" align="center">
- <div align="center" style="padding: 0px; margin: 0px; width: 1024px;"
- id="div1" runat="server">
- 商品名:<input id="txtGoodsName" type="text" runat="server"/>
- 商品规格:<input id="txtStander" type="text" runat="server"/>
- 商品产地:<input id="txtHabitat" type="text" runat="server"/>
- 商品数量:<input id="txtGoodsCount" type="text" runat="server"/>
- 商品价格:<input id="txtPrice" type="text" runat="server"/>
- <br />
- 生产日期:<input id="txtProduce" type="text" runat="server" οnclick="WdatePicker();"/>
- 净含量:<input id="txtWeight" type="text" runat="server" /> 商品类型:<asp:DropDownList
- ID="ddlType" runat="server" DataSourceID="ObjectDataSource1"
- DataTextField="GoodsTypeName" DataValueField="GoodsTypeId">
- </asp:DropDownList>
- 单位:<asp:DropDownList ID="ddlUnit" runat="server"
- DataSourceID="ObjectDataSource2" DataTextField="Mass" DataValueField="UnitId">
- </asp:DropDownList>
- <asp:Button ID="btnAdd" runat="server"
- οnclick="btnAdd_Click" Text="添 加" />
- <asp:Button ID="btnUpdate" runat="server" Text="修 改"
- οnclick="btnUpdate_Click" />
- <br />
- </div>
- <input id="hidGoods" type="hidden" runat="server" />
- <div align="center" style="margin-top: 6px; width: 1024px">
- <asp:GridView ID="gvGoodsInfo" runat="server" AutoGenerateColumns="False" CellPadding="3"
- GridLines="Horizontal" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"
- BorderWidth="1px" Width="1024px" onrowcommand="gvGoodsInfo_RowCommand"
- DataKeyNames="goodsid" onrowdatabound="gvGoodsInfo_RowDataBound">
- <Columns>
- <asp:BoundField DataField="GoodsId" HeaderText="商品编号" SortExpression="GoodsId" />
- <asp:BoundField DataField="GoodsName" HeaderText="商品名称" SortExpression="GoodsName" />
- <asp:TemplateField HeaderText="商品类型" SortExpression="GoodsType">
- <ItemTemplate>
- <asp:Label ID="Label2" runat="server" Text='<%# Eval("GoodsType.GoodsTypeName") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:BoundField DataField="Standard" HeaderText="规格" SortExpression="Standard" />
- <asp:BoundField DataField="Habitat" HeaderText="产地" SortExpression="Habitat" />
- <asp:BoundField DataField="GoodsCount" HeaderText="数量" SortExpression="GoodsCount" />
- <asp:TemplateField HeaderText="单位" SortExpression="Unit">
- <ItemTemplate>
- <asp:Label ID="Label1" runat="server" Text='<%# Eval("Units.Mass") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:BoundField DataField="Weight" HeaderText="净含量" SortExpression="Weight" />
- <asp:BoundField DataField="price" HeaderText="价格" DataFormatString="{0:c2}" />
- <asp:TemplateField HeaderText="生产日期" SortExpression="ProductTime">
- <ItemTemplate>
- <asp:Label ID="Label3" runat="server" Text='<%# ToShortDate(Eval("ProductTime")) %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:BoundField DataField="PledgeDate" HeaderText="保质期" SortExpression="PledgeDate" />
- <asp:BoundField DataField="GoodsAddTime" HeaderText="添加时间" SortExpression="GoodsAddTime" />
- <asp:TemplateField HeaderText="修改">
- <ItemTemplate>
- <asp:ImageButton ID="ibtnEdit" runat="server"
- CommandName="updateGoods" ImageUrl="~/Images/edit.gif" />
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="删除">
- <ItemTemplate>
- <asp:ImageButton ID="ibtnDelete" runat="server" CommandName=" GoodsDelete"
- ImageUrl="~/Images/delete.gif" />
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- <RowStyle HorizontalAlign="Center" BackColor="#E7E7FF" ForeColor="#4A3C8C" />
- <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
- <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
- <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
- <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
- <AlternatingRowStyle BackColor="#F7F7F7" />
- </asp:GridView>
- <hr size="1" style="color: #9900FF" />
- </div>
- </div>
- <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetAllUnits"
- TypeName="Account.BLL.UnitManager"></asp:ObjectDataSource>
- <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetAllGoodsTypes"
- TypeName="Account.BLL.GoodsTypeManager"></asp:ObjectDataSource>
- <asp:HiddenField ID="HiddenField1" runat="server" />
- </form>
- </body>
- </html>
后台代码
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using Account.BLL;
- using Account.Models;
- public partial class GoodsManage : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- DisplayGoodsInfo();
- }
- private void DisplayGoodsInfo()
- {
- gvGoodsInfo.DataSource = GoodsManager.GetAllGoods();
- gvGoodsInfo.DataBind();
- }
- public static String ToShortDate(object obj)
- {
- return Convert.ToDateTime(obj).ToShortDateString();
- }
- protected void btnAdd_Click(object sender, EventArgs e)
- {
- #region 非空验证
- if (txtGoodsName.Value == "")
- {
- this.Page.ClientScript.RegisterStartupScript(this.GetType(), "OK", "<script>alert('请输入商品名!')</script>");
- txtGoodsName.Focus();
- return;
- }
- if (txtStander.Value == "")
- {
- this.Page.ClientScript.RegisterStartupScript(this.GetType(), "OK", "<script>alert('请输入商品规格!')</script>");
- txtStander.Focus();
- return;
- }
- if (txtHabitat.Value == "")
- {
- this.Page.ClientScript.RegisterStartupScript(this.GetType(), "OK", "<script>alert('请输入商品产地!')</script>");
- txtHabitat.Focus();
- return;
- }
- if (txtGoodsCount.Value == "")
- {
- this.Page.ClientScript.RegisterStartupScript(this.GetType(), "OK", "<script>alert('请输入商品数量!')</script>");
- txtGoodsCount.Focus();
- return;
- }
- if (txtPrice.Value == "")
- {
- this.Page.ClientScript.RegisterStartupScript(this.GetType(), "OK", "<script>alert('请输入商品规格!')</script>");
- txtPrice.Focus();
- return;
- }
- if (txtProduce.Value == "")
- {
- this.Page.ClientScript.RegisterStartupScript(this.GetType(), "OK", "<script>alert('请输入商品生产日期!')</script>");
- txtProduce.Focus();
- return;
- }
- if (txtWeight.Value == "")
- {
- this.Page.ClientScript.RegisterStartupScript(this.GetType(), "OK", "<script>alert('请输入商品净含量!')</script>");
- txtWeight.Focus();
- return;
- }
- #endregion
- Goods goods = new Goods();
- GoodsTypes goodsType = new GoodsTypes();
- goodsType.GoodsTypeId = int.Parse(ddlType.SelectedValue);
- goods.GoodsType = goodsType;
- goods.GoodsCount = double.Parse(txtGoodsCount.Value);
- goods.GoodsName = txtGoodsName.Value;
- goods.Habitat = txtHabitat.Value;
- goods.Price = decimal.Parse(txtPrice.Value);
- goods.Standard = txtStander.Value;
- goods.Weight = double.Parse(txtWeight.Value);
- goods.ProductTime = DateTime.Parse(txtProduce.Value.ToString());
- Units units = new Units();
- units.UnitId = int.Parse(ddlUnit.SelectedValue);
- goods.Units = units;
- goods.GoodsAddTime = DateTime.Now;
- if (ddlType.SelectedValue.Equals("3"))
- {
- goods.PledgeDate = "1年";
- }
- else
- {
- goods.PledgeDate = "2年";
- }
- GoodsManager.AddGood(goods);
- DisplayGoodsInfo();
- }
- protected void gvGoodsInfo_RowCommand(object sender, GridViewCommandEventArgs e)
- {
- if (e.CommandName == "GoodsDelete")
- {
- User user = (User)Session["CurrentUser"];
- GoodsManager.DeleteGoodByGoodsId(int.Parse(e.CommandArgument.ToString()));
- OperateLogs operateLog = new OperateLogs();
- operateLog.OperateDesc = "删除商品";
- operateLog.OperateName = "删除";
- operateLog.OperateTime = DateTime.Now;
- operateLog.User = user;
- OperateLogManager.AddOperateLog(operateLog);
- DisplayGoodsInfo();
- txtGoodsCount.Value = "";
- txtGoodsName.Value = "";
- txtHabitat.Value = "";
- txtPrice.Value = "";
- txtProduce.Value = "";
- txtStander.Value = "";
- txtWeight.Value = "";
- }
- if (e.CommandName=="updateGoods")
- {
- int goodsId = int.Parse(e.CommandArgument.ToString());
- Goods goods = GoodsManager.GetGoodByGoodsId(goodsId);
- HiddenField1.Value = goodsId.ToString();
- txtGoodsCount.Value = goods.GoodsCount.ToString();
- txtGoodsName.Value = goods.GoodsName;
- txtHabitat.Value = goods.Habitat;
- txtPrice.Value = goods.Price.ToString();
- txtProduce.Value = goods.ProductTime.ToShortDateString();
- txtStander.Value = goods.Standard;
- txtWeight.Value = goods.Weight.ToString();
- btnAdd.Enabled = false;
- }
- }
- protected void gvGoodsInfo_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowType==DataControlRowType.DataRow)
- {
- string goodsId = gvGoodsInfo.DataKeys[e.Row.RowIndex].Value.ToString();
- ImageButton ibtnDelete = (ImageButton)e.Row.FindControl("ibtnDelete");
- ibtnDelete.CommandArgument = goodsId;
- ibtnDelete.Attributes.Add("onclick", "return confirm('确定要删除吗?');");
- ImageButton ibtnEdit = (ImageButton)e.Row.FindControl("ibtnEdit");
- ibtnEdit.CommandArgument = goodsId;
- }
- }
- protected void btnUpdate_Click(object sender, EventArgs e)
- {
- Goods goods = new Goods();
- goods.GoodsAddTime = DateTime.Now;
- goods.GoodsCount =double.Parse( txtGoodsCount.Value);
- goods.GoodsName = txtGoodsName.Value;
- goods.Habitat = txtHabitat.Value;
- goods.Price =decimal.Parse( txtPrice.Value);
- goods.ProductTime =DateTime.Parse( txtProduce.Value);
- goods.Standard = txtStander.Value;
- goods.Weight =double.Parse( txtWeight.Value);
- Units units = new Units();
- GoodsTypes goodstype = new GoodsTypes();
- goods.Units = units;
- goods.GoodsType = goodstype;
- goods.GoodsId =int.Parse( HiddenField1.Value);
- GoodsManager.ModifyGood(goods);
- }
- }