在ASP.NET GridView 中使用e.CommandArgument传递参数

1、前台设置参数:

//...

<asp:TemplateField ItemStyle-HorizontalAlign="center" ItemStyle-Width="80px">
                            
<ItemTemplate>
                                
<asp:Button ID="btnAllocateRole" Text="分配角色" runat="server" CommandName="AllocateRole" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "UserId")%>' BorderStyle="Groove"/>
                            
</ItemTemplate>
                            
</asp:TemplateField>

//...

说明:我这里是从后台绑定的一个ID号

2、后台使用:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
       
if (e.CommandName == "AllocateRole"//分配角色
        {
            String sUserId 
= e.CommandArgument.ToString();   //这里获取ID号
              Response.Redirect("Admin_UserRole_Edit.aspx?ID=" + sUserId);
       }
}
原文地址:https://www.cnblogs.com/xvqm00/p/1715553.html