• 获取GridView中RowCommand的当前索引行


    获取GridView中RowCommand的当前索引行 前台添加一模版列,里面添加一个LinkButton

    前台 (如果在后台代码中用e.CommandArgument取值的话前台代码就必须在按钮中设置CommandArgument的值,值为绑定的数据库字段

    1 <asp:TemplateField HeaderText="操作">
    2     <ItemTemplate>
    3         <asp:LinkButton ID="LinkButton1" runat="server" CommandName="QianRu" 
    4         CommandArgument='<%# Eval("Id") %>'>签入</asp:LinkButton>  
    5         <asp:LinkButton ID="LinkButton2" runat="server" CommandName="QianChu">签出</asp:LinkButton>
    6     </ItemTemplate>
    7 </asp:TemplateField>

    后台

    在GridView里已经设置了LinkButton为事件处理按钮,将通过以下方法获取索引

     1   protected void gv_Company_RowCommand(object sender, GridViewCommandEventArgs e)
     2   {
     3       if (e.CommandName == "QianRu")
     4       {     //取ID的值方法一   
     5           GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值
     6           inf id = Convert.ToInt32(GridView1.DataKeys[drv.RowIndex].Value); //此获取的值为GridView中绑定数据库中的主键值
     7 
     8           //取ID的值方法二   
     9            GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值
    10           //此获取的值为GridView中绑定数据库中的主键值,取值方法是选中的行中的第一列的值,drv.RowIndex取得是选中行的索引
    11            int id = Convert.ToInt32(GridView1.Rows[drv.RowIndex].Cells[0].Text);
    12 
    13           //取ID的值方法三  
    14           //因为在客户端中就已经将LinkButton的CommandArgument与主键Id给绑定了所以在此可以直接用e.CommandArgument得出主键ID的值
    15             int id = Convert.ToInt32(e.CommandArgument.ToString());
    16       }
    17  }

    还有一种就是我们并不需要知道当前点击的是第几行,可以用以下方法实现要求:

    1 <ItemTemplate> 
    2       <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument=' <%# Eval("field1") %>' 
    3       CommandName="play" Text=' <%# Eval("field2") %>'> </asp:LinkButton> 
    4 </ItemTemplate> 

    上面这个LinkButton,Text绑定了字段2, CommandArgument绑定了字段1 

    那么,

    1 protected  void  GridView1_RowCommand(object  sender,  GridViewCommandEventArgs  e) 
    2 { 
    3     if(e.CommandName="play")
    4     {
    5         LinkButton lb = (LinkButton)e.CommandSource; 
    6         string  a  =  lb.Text;//这里可以获得点击行字段field2的值 
    7         string b = e.CommandArgument;//这里可以获得点击行字段field1的值
    8     }
    9 }

    或:

    如果是使用模板列,可以把数据的任意一列绑定到按钮的CommandArgument,如下: 

    1 <asp:TemplateField> 
    2       <ItemTemplate> 
    3       <asp:Button runat="server" CommandArgument='<%# Eval("id") %>' Text="Button" /> 
    4       </ItemTemplate> 
    5 </asp:TemplateField> 

    一般可以绑定到主键列,这样可以在RowCommand通过e.CommandArgument获取当前行的主键,也便于进行其他操作 

    如果是要获取行索引,比较麻烦一点,还是那个Button1,在GridView的RowDataBound事件中如下: 

    1 Button btn = (Button)e.Row.FindControl("Button1"); 
    2 if (btn != null) 
    3 { 
    4   btn.CommandArgument = e.Row.RowIndex.ToString(); 
    5 } 

    这样就可以在RowCommand中通过 int rowId=Convert.ToInt32(e.CommandArgument.ToString()) 获取行索引了 

  • 相关阅读:
    Nodejs 开发指南 Nodejs+Express+ejs 开发microblog开发心得
    转载 java学习注意点
    STM32f103的数电采集电路的ADC多通道采集程序
    时间复杂度与空间复杂度
    RS232串口通信详解
    实现扫码登录
    TCP/UDP区别与联系
    Tcp三次握手/四次挥手
    浅谈CSRF攻击方式
    图片淡入淡出
  • 原文地址:https://www.cnblogs.com/lgx5/p/12096816.html
Copyright © 2020-2023  润新知