asp.net aspx页面中的GridView的RowCommand怎么不能触发

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GoodsManage.aspx.cs" Inherits="GoodsManage"  
  2.     EnableEventValidation="false" %>  
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title>商品管理</title>  
  8.     <link href="css/Style.css" rel="stylesheet" type="text/css" />  
  9.   
  10.     <script language="javascript" type="text/javascript" src="My97DatePicker/WdatePicker.js"></script>  
  11.   
  12. </head>  
  13. <body>  
  14.     <form id="form1" runat="server">  
  15.     <div style="padding: 0px; margin: 0px" align="center">  
  16.         <div align="center" style="padding: 0px; margin: 0px;  width: 1024px;"  
  17.             id="div1" runat="server">  
  18.             商品名:<input id="txtGoodsName" type="text" runat="server"/>&nbsp;&nbsp;&nbsp;&nbsp;  
  19.             商品规格:<input id="txtStander" type="text" runat="server"/>  
  20.             商品产地:<input id="txtHabitat" type="text" runat="server"/>  
  21.             商品数量:<input id="txtGoodsCount" type="text" runat="server"/>  
  22.             商品价格:<input id="txtPrice" type="text" runat="server"/>  
  23.             <br />  
  24.              生产日期:<input id="txtProduce" type="text" runat="server" οnclick="WdatePicker();"/>  
  25.               净含量:<input id="txtWeight" type="text" runat="server" /> 商品类型:<asp:DropDownList   
  26.                 ID="ddlType" runat="server" DataSourceID="ObjectDataSource1"   
  27.                 DataTextField="GoodsTypeName" DataValueField="GoodsTypeId">  
  28.             </asp:DropDownList>  
  29.             单位:<asp:DropDownList ID="ddlUnit" runat="server"   
  30.                 DataSourceID="ObjectDataSource2" DataTextField="Mass" DataValueField="UnitId">  
  31.             </asp:DropDownList>  
  32.             <asp:Button ID="btnAdd" runat="server"   
  33.                 οnclick="btnAdd_Click" Text="添 加" />  
  34.             &nbsp;<asp:Button ID="btnUpdate" runat="server" Text="修 改"   
  35.                 οnclick="btnUpdate_Click" />  
  36.             <br />  
  37.         </div>  
  38.         <input id="hidGoods" type="hidden" runat="server" />  
  39.         <div align="center" style="margin-top: 6px; width: 1024px">  
  40.             <asp:GridView ID="gvGoodsInfo" runat="server" AutoGenerateColumns="False" CellPadding="3"  
  41.                 GridLines="Horizontal" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"  
  42.                 BorderWidth="1px" Width="1024px" onrowcommand="gvGoodsInfo_RowCommand"   
  43.                 DataKeyNames="goodsid" onrowdatabound="gvGoodsInfo_RowDataBound">  
  44.                 <Columns>  
  45.                     <asp:BoundField DataField="GoodsId" HeaderText="商品编号" SortExpression="GoodsId" />  
  46.                     <asp:BoundField DataField="GoodsName" HeaderText="商品名称" SortExpression="GoodsName" />  
  47.                     <asp:TemplateField HeaderText="商品类型" SortExpression="GoodsType">  
  48.                         <ItemTemplate>  
  49.                             <asp:Label ID="Label2" runat="server" Text='<%# Eval("GoodsType.GoodsTypeName") %>'></asp:Label>  
  50.                         </ItemTemplate>  
  51.                     </asp:TemplateField>  
  52.                     <asp:BoundField DataField="Standard" HeaderText="规格" SortExpression="Standard" />  
  53.                     <asp:BoundField DataField="Habitat" HeaderText="产地" SortExpression="Habitat" />  
  54.                     <asp:BoundField DataField="GoodsCount" HeaderText="数量" SortExpression="GoodsCount" />  
  55.                     <asp:TemplateField HeaderText="单位" SortExpression="Unit">  
  56.                         <ItemTemplate>  
  57.                             <asp:Label ID="Label1" runat="server" Text='<%# Eval("Units.Mass") %>'></asp:Label>  
  58.                         </ItemTemplate>  
  59.                     </asp:TemplateField>  
  60.                     <asp:BoundField DataField="Weight" HeaderText="净含量" SortExpression="Weight" />  
  61.                     <asp:BoundField DataField="price" HeaderText="价格" DataFormatString="{0:c2}" />  
  62.                     <asp:TemplateField HeaderText="生产日期" SortExpression="ProductTime">  
  63.                         <ItemTemplate>  
  64.                             <asp:Label ID="Label3" runat="server" Text='<%# ToShortDate(Eval("ProductTime")) %>'></asp:Label>  
  65.                         </ItemTemplate>  
  66.                     </asp:TemplateField>  
  67.                     <asp:BoundField DataField="PledgeDate" HeaderText="保质期" SortExpression="PledgeDate" />  
  68.                     <asp:BoundField DataField="GoodsAddTime" HeaderText="添加时间" SortExpression="GoodsAddTime" />  
  69.                     <asp:TemplateField HeaderText="修改">  
  70.                         <ItemTemplate>  
  71.                             <asp:ImageButton ID="ibtnEdit" runat="server"  
  72.                                 CommandName="updateGoods" ImageUrl="~/Images/edit.gif" />  
  73.                         </ItemTemplate>  
  74.                     </asp:TemplateField>  
  75.                     <asp:TemplateField HeaderText="删除">  
  76.                         <ItemTemplate>  
  77.                             <asp:ImageButton ID="ibtnDelete" runat="server" CommandName=" GoodsDelete"  
  78.                                 ImageUrl="~/Images/delete.gif"  />  
  79.                         </ItemTemplate>  
  80.                     </asp:TemplateField>  
  81.                 </Columns>  
  82.                 <RowStyle HorizontalAlign="Center" BackColor="#E7E7FF" ForeColor="#4A3C8C" />  
  83.                 <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />  
  84.                 <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />  
  85.                 <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />  
  86.                 <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />  
  87.                 <AlternatingRowStyle BackColor="#F7F7F7" />  
  88.             </asp:GridView>  
  89.             <hr size="1" style="color: #9900FF" />  
  90.         </div>  
  91.     </div>  
  92.     <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetAllUnits"  
  93.         TypeName="Account.BLL.UnitManager"></asp:ObjectDataSource>  
  94.     <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetAllGoodsTypes"  
  95.         TypeName="Account.BLL.GoodsTypeManager"></asp:ObjectDataSource>  
  96.     <asp:HiddenField ID="HiddenField1" runat="server" />  
  97.     </form>  
  98. </body>  
  99. </html>  

 后台代码

C#代码   收藏代码
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Collections;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;  
  10. using System.Web.UI.HtmlControls;  
  11. using Account.BLL;  
  12. using Account.Models;  
  13.   
  14. public partial class GoodsManage : System.Web.UI.Page  
  15. {  
  16.     protected void Page_Load(object sender, EventArgs e)  
  17.     {  
  18.   
  19.         DisplayGoodsInfo();  
  20.     }  
  21.   
  22.     private void DisplayGoodsInfo()  
  23.     {  
  24.         gvGoodsInfo.DataSource = GoodsManager.GetAllGoods();  
  25.         gvGoodsInfo.DataBind();  
  26.     }  
  27.   
  28.     public static String ToShortDate(object obj)  
  29.     {  
  30.         return Convert.ToDateTime(obj).ToShortDateString();  
  31.     }  
  32.     protected void btnAdd_Click(object sender, EventArgs e)  
  33.     {  
  34.         #region 非空验证  
  35.   
  36.         if (txtGoodsName.Value == "")  
  37.         {  
  38.             this.Page.ClientScript.RegisterStartupScript(this.GetType(), "OK""<script>alert('请输入商品名!')</script>");  
  39.             txtGoodsName.Focus();  
  40.             return;  
  41.         }  
  42.         if (txtStander.Value == "")  
  43.         {  
  44.             this.Page.ClientScript.RegisterStartupScript(this.GetType(), "OK""<script>alert('请输入商品规格!')</script>");  
  45.             txtStander.Focus();  
  46.             return;  
  47.         }  
  48.         if (txtHabitat.Value == "")  
  49.         {  
  50.             this.Page.ClientScript.RegisterStartupScript(this.GetType(), "OK""<script>alert('请输入商品产地!')</script>");  
  51.             txtHabitat.Focus();  
  52.             return;  
  53.         }  
  54.         if (txtGoodsCount.Value == "")  
  55.         {  
  56.             this.Page.ClientScript.RegisterStartupScript(this.GetType(), "OK""<script>alert('请输入商品数量!')</script>");  
  57.             txtGoodsCount.Focus();  
  58.             return;  
  59.         }  
  60.         if (txtPrice.Value == "")  
  61.         {  
  62.             this.Page.ClientScript.RegisterStartupScript(this.GetType(), "OK""<script>alert('请输入商品规格!')</script>");  
  63.             txtPrice.Focus();  
  64.             return;  
  65.         }  
  66.         if (txtProduce.Value == "")  
  67.         {  
  68.             this.Page.ClientScript.RegisterStartupScript(this.GetType(), "OK""<script>alert('请输入商品生产日期!')</script>");  
  69.             txtProduce.Focus();  
  70.             return;  
  71.         }  
  72.         if (txtWeight.Value == "")  
  73.         {  
  74.             this.Page.ClientScript.RegisterStartupScript(this.GetType(), "OK""<script>alert('请输入商品净含量!')</script>");  
  75.             txtWeight.Focus();  
  76.             return;  
  77.         }  
  78.         #endregion  
  79.   
  80.         Goods goods = new Goods();  
  81.         GoodsTypes goodsType = new GoodsTypes();  
  82.         goodsType.GoodsTypeId = int.Parse(ddlType.SelectedValue);  
  83.         goods.GoodsType = goodsType;  
  84.         goods.GoodsCount = double.Parse(txtGoodsCount.Value);  
  85.         goods.GoodsName = txtGoodsName.Value;  
  86.         goods.Habitat = txtHabitat.Value;  
  87.         goods.Price = decimal.Parse(txtPrice.Value);  
  88.         goods.Standard = txtStander.Value;  
  89.         goods.Weight = double.Parse(txtWeight.Value);  
  90.         goods.ProductTime = DateTime.Parse(txtProduce.Value.ToString());  
  91.         Units units = new Units();  
  92.         units.UnitId = int.Parse(ddlUnit.SelectedValue);  
  93.         goods.Units = units;  
  94.         goods.GoodsAddTime = DateTime.Now;  
  95.         if (ddlType.SelectedValue.Equals("3"))  
  96.         {  
  97.             goods.PledgeDate = "1年";  
  98.         }  
  99.         else  
  100.         {  
  101.             goods.PledgeDate = "2年";  
  102.         }  
  103.         GoodsManager.AddGood(goods);  
  104.         DisplayGoodsInfo();  
  105.     }  
  106.     protected void gvGoodsInfo_RowCommand(object sender, GridViewCommandEventArgs e)  
  107.     {  
  108.         if (e.CommandName == "GoodsDelete")  
  109.         {  
  110.             User user = (User)Session["CurrentUser"];  
  111.             GoodsManager.DeleteGoodByGoodsId(int.Parse(e.CommandArgument.ToString()));  
  112.             OperateLogs operateLog = new OperateLogs();  
  113.             operateLog.OperateDesc = "删除商品";  
  114.             operateLog.OperateName = "删除";  
  115.             operateLog.OperateTime = DateTime.Now;  
  116.             operateLog.User = user;  
  117.             OperateLogManager.AddOperateLog(operateLog);  
  118.   
  119.             DisplayGoodsInfo();  
  120.             txtGoodsCount.Value = "";  
  121.             txtGoodsName.Value = "";  
  122.             txtHabitat.Value = "";  
  123.             txtPrice.Value = "";  
  124.             txtProduce.Value = "";  
  125.             txtStander.Value = "";  
  126.             txtWeight.Value = "";  
  127.         }  
  128.         if (e.CommandName=="updateGoods")  
  129.         {  
  130.             int goodsId = int.Parse(e.CommandArgument.ToString());  
  131.             Goods goods = GoodsManager.GetGoodByGoodsId(goodsId);  
  132.   
  133.             HiddenField1.Value = goodsId.ToString();  
  134.             txtGoodsCount.Value = goods.GoodsCount.ToString();  
  135.             txtGoodsName.Value = goods.GoodsName;  
  136.             txtHabitat.Value = goods.Habitat;  
  137.             txtPrice.Value = goods.Price.ToString();  
  138.             txtProduce.Value = goods.ProductTime.ToShortDateString();  
  139.             txtStander.Value = goods.Standard;  
  140.             txtWeight.Value = goods.Weight.ToString();  
  141.             btnAdd.Enabled = false;  
  142.         }  
  143.     }  
  144.     protected void gvGoodsInfo_RowDataBound(object sender, GridViewRowEventArgs e)  
  145.     {  
  146.         if (e.Row.RowType==DataControlRowType.DataRow)  
  147.         {  
  148.             string goodsId = gvGoodsInfo.DataKeys[e.Row.RowIndex].Value.ToString();  
  149.             ImageButton ibtnDelete = (ImageButton)e.Row.FindControl("ibtnDelete");  
  150.             ibtnDelete.CommandArgument = goodsId;  
  151.             ibtnDelete.Attributes.Add("onclick""return confirm('确定要删除吗?');");  
  152.             ImageButton ibtnEdit = (ImageButton)e.Row.FindControl("ibtnEdit");  
  153.             ibtnEdit.CommandArgument = goodsId;  
  154.         }  
  155.     }  
  156.     protected void btnUpdate_Click(object sender, EventArgs e)  
  157.     {  
  158.         Goods goods = new Goods();  
  159.         goods.GoodsAddTime = DateTime.Now;  
  160.         goods.GoodsCount =double.Parse( txtGoodsCount.Value);  
  161.         goods.GoodsName = txtGoodsName.Value;  
  162.           
  163.         goods.Habitat = txtHabitat.Value;  
  164.         goods.Price =decimal.Parse( txtPrice.Value);  
  165.         goods.ProductTime =DateTime.Parse( txtProduce.Value);  
  166.         goods.Standard = txtStander.Value;  
  167.         goods.Weight =double.Parse( txtWeight.Value);  
  168.   
  169.         Units units = new Units();  
  170.         GoodsTypes goodstype = new GoodsTypes();  
  171.         goods.Units = units;  
  172.         goods.GoodsType = goodstype;  
  173.         goods.GoodsId =int.Parse( HiddenField1.Value);  
  174.         GoodsManager.ModifyGood(goods);  
  175.     }  
  176. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值