获取模版列中按钮事件所在行的关键字

被常常问到的一个经典问题了,答案放到网上来。
步骤
1 ASP.NET的这个地方这样设置。GridView加入 OnRowCommand 事件,模版列的按钮设置CommandArgument、CommandName
         < asp:GridView  ID ="GridView1"  runat ="server"  CellPadding ="4"  AutoGenerateColumns ="False"  OnRowCommand ="GridView1_RowCommand" >
            
< Columns >
                
< asp:BoundField  DataField ="id"  HeaderText ="编号"   />
                
< asp:BoundField  DataField ="loginID"  HeaderText ="登陆名"   />
                
< asp:TemplateField  HeaderText ="工具栏权限管理" >
                    
< ItemTemplate >
                        
< asp:Label  ID ="Lblqx1"  runat ="server" ></ asp:Label >
                        
< asp:Button  ID ="BtnGjl"  runat ="server"  Text ="设置"  CommandArgument ='<%#Eval("id")  % > '  CommandName="gjl" />
                    
</ ItemTemplate >
                
</ asp:TemplateField >
            
</ Columns >
        
</ asp:GridView >

 2 后台cs的GridView1_RowCommand方法

     protected   void  GridView1_RowCommand( object  sender, GridViewCommandEventArgs e)
    
{
        
if (e.CommandName == "gjl")
        
{
            Response.Redirect(
"NewPage.aspx?key=" + e.CommandArgument.ToString(), false);
        }
 
    }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值