1. RowCommond 事件
.aspx
在GridView添加一模板列,加上CommandArgument 并绑定如下
- <asp:GridView ID="gvAdminList" runat="server" >
- <asp:TemplateField>
- <ItemTemplate>
- <asp:LinkButton ID="lbtnLook" runat="server" CommandName="GetAdminInfo" CommandArgument='<%# Eval("AdminID") %>'>查看</asp:LinkButton>
- </ItemTemplate>
- </asp:TemplateField>
- </asp:GridView>
.aspx.cs
读取CommandArgument
- protected void gvAdminList_RowCommand(object sender, GridViewCommandEventArgs e)
- {
- if (e.CommandName == "GetAdminInfo")
- {
- string AdminID = e.CommandArgument.ToString();
- }
- }
这个事件是我们常用的获取主键的方法,通过为按钮设置 commandname和 commandargument ,前者是用来区别点了哪个按钮,后者则是按钮所代表的参数,利用绑定,很容易获取了要操作行的主键