获取模版列中按钮事件所在行的关键字
被常常问到的一个经典问题了,答案放到网上来。
步骤1 ASP.NET的这个地方这样设置。GridView加入 OnRowCommand 事件,模版列的按钮设置CommandArgument、CommandName
1<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound ="dgItemDataBound"
2 PageSize="15" Width="100%" AllowPaging="True" BorderWidth="0px" CellPadding="2" CellSpacing="1" OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing" OnRowCreated="GridView1_RowCreated" OnRowDeleting="GridView1_RowDeleting" >
3 <Columns>
4 <asp:BoundField DataField="TableField_ID" HeaderText="编号" Visible=False />
5 <asp:BoundField DataField="TbName" HeaderText="表名称" />
6 <asp:HyperLinkField DataTextField="FdName" SortExpression="FdName" HeaderText="字段名称" DataNavigateUrlFields="FdName" DataNavigateUrlFormatString="DdCreateField.aspx?FdName={0}"></asp:HyperLinkField>
7 <asp:HyperLinkField DataTextField="Descr" SortExpression="Descr" HeaderText="字段描述" DataNavigateUrlFields="FdName" DataNavigateUrlFormatString="DdCreateField.aspx?FdName={0}"></asp:HyperLinkField>
8 <asp:BoundField DataField="SequenceNo" HeaderText="顺序" />
9 <asp:BoundField DataField="KeyField" HeaderText="主键" />
10 <asp:TemplateField HeaderText="增加">
11 <ItemTemplate>
12 <asp:ImageButton CommandArgument='<%# DataBinder.Eval(Container.DataItem,"TbName") %>' id=ImageButton1 runat="server" ImageUrl='../images/icon_add.gif' CommandName="Add" ImageAlign="AbsMiddle">
13 </asp:ImageButton>
14 </ItemTemplate>
15 <ItemStyle HorizontalAlign="Center" Width="30px" />
16 </asp:TemplateField>
17 <asp:TemplateField HeaderText="修改">
18 <ItemTemplate>
19 <asp:ImageButton id=ImageButton2 runat="server" ImageUrl='../images/icon_edit.gif' CommandName="Edit" ImageAlign="AbsMiddle">
20 </asp:ImageButton>
21 </ItemTemplate>
22 <ItemStyle HorizontalAlign="Center" Width="30px" />
23 </asp:TemplateField>
24 <asp:TemplateField HeaderText="删除">
25 <ItemTemplate>
26 <asp:ImageButton id=ImageButton3 runat="server" ImageUrl='../images/icon_delete.gif' CommandName="Delete" ImageAlign="AbsMiddle">
27 </asp:ImageButton>
28 </ItemTemplate>
29 <ItemStyle HorizontalAlign="Center" Width="30px" />
30 </asp:TemplateField>
31
32 </Columns>
33 <RowStyle BackColor="White" HorizontalAlign="Left" />
34 <HeaderStyle CssClass="B6BBDA" HorizontalAlign="Center" />
35 <PagerTemplate>
36 第 [
37 <asp:Label ID="Label1" runat="server" Text="<%# GridView1.PageIndex+1 %>"></asp:Label>
38 ] 页
39 </PagerTemplate>
40 </asp:GridView>
2 PageSize="15" Width="100%" AllowPaging="True" BorderWidth="0px" CellPadding="2" CellSpacing="1" OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing" OnRowCreated="GridView1_RowCreated" OnRowDeleting="GridView1_RowDeleting" >
3 <Columns>
4 <asp:BoundField DataField="TableField_ID" HeaderText="编号" Visible=False />
5 <asp:BoundField DataField="TbName" HeaderText="表名称" />
6 <asp:HyperLinkField DataTextField="FdName" SortExpression="FdName" HeaderText="字段名称" DataNavigateUrlFields="FdName" DataNavigateUrlFormatString="DdCreateField.aspx?FdName={0}"></asp:HyperLinkField>
7 <asp:HyperLinkField DataTextField="Descr" SortExpression="Descr" HeaderText="字段描述" DataNavigateUrlFields="FdName" DataNavigateUrlFormatString="DdCreateField.aspx?FdName={0}"></asp:HyperLinkField>
8 <asp:BoundField DataField="SequenceNo" HeaderText="顺序" />
9 <asp:BoundField DataField="KeyField" HeaderText="主键" />
10 <asp:TemplateField HeaderText="增加">
11 <ItemTemplate>
12 <asp:ImageButton CommandArgument='<%# DataBinder.Eval(Container.DataItem,"TbName") %>' id=ImageButton1 runat="server" ImageUrl='../images/icon_add.gif' CommandName="Add" ImageAlign="AbsMiddle">
13 </asp:ImageButton>
14 </ItemTemplate>
15 <ItemStyle HorizontalAlign="Center" Width="30px" />
16 </asp:TemplateField>
17 <asp:TemplateField HeaderText="修改">
18 <ItemTemplate>
19 <asp:ImageButton id=ImageButton2 runat="server" ImageUrl='../images/icon_edit.gif' CommandName="Edit" ImageAlign="AbsMiddle">
20 </asp:ImageButton>
21 </ItemTemplate>
22 <ItemStyle HorizontalAlign="Center" Width="30px" />
23 </asp:TemplateField>
24 <asp:TemplateField HeaderText="删除">
25 <ItemTemplate>
26 <asp:ImageButton id=ImageButton3 runat="server" ImageUrl='../images/icon_delete.gif' CommandName="Delete" ImageAlign="AbsMiddle">
27 </asp:ImageButton>
28 </ItemTemplate>
29 <ItemStyle HorizontalAlign="Center" Width="30px" />
30 </asp:TemplateField>
31
32 </Columns>
33 <RowStyle BackColor="White" HorizontalAlign="Left" />
34 <HeaderStyle CssClass="B6BBDA" HorizontalAlign="Center" />
35 <PagerTemplate>
36 第 [
37 <asp:Label ID="Label1" runat="server" Text="<%# GridView1.PageIndex+1 %>"></asp:Label>
38 ] 页
39 </PagerTemplate>
40 </asp:GridView>
还可以使用CommandArgument='<%# Bind("TbName") %>'
2 后台cs的GridView1_RowCommand方法
1 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
2 {
3 if (e.CommandName == "Add")
4 {
5 string index = Convert.ToString(e.CommandArgument);
6 }
7 else if (e.CommandName == "Edit")
8 {
9 }
10 else if (e.CommandName == "Delete")
11 {
12 }
13 }
2 {
3 if (e.CommandName == "Add")
4 {
5 string index = Convert.ToString(e.CommandArgument);
6 }
7 else if (e.CommandName == "Edit")
8 {
9 }
10 else if (e.CommandName == "Delete")
11 {
12 }
13 }