如果是使用模板列,可以把数据的任意一列绑定到按钮的CommandArgument,如下:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CommandArgument='<%# Eval("id") %>' Text="Button" />
</ItemTemplate>
</asp:TemplateField>
一般可以绑定到主键列,这样可以在RowCommand通过e.CommandArgument获取当前行的主键,也便于进行其他操作
如果是要获取行索引,比较麻烦一点,还是那个Button1,在GridView的RowDataBound事件中如下:
Button btn = (Button)e.Row.FindControl("Button1");
if (btn != null)
{
btn.CommandArgument = e.Row.RowIndex.ToString();
}
这样就可以在RowCommand中通过e.CommandArgument获取行索引了
不过感觉用行索引的时候比较少,一般都是通过主键的

相关文章:

  • 2021-12-21
  • 2022-02-28
  • 2022-12-23
  • 2021-08-04
  • 2021-09-27
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-19
  • 2021-08-29
  • 2021-05-29
相关资源
相似解决方案