zoukankan      html  css  js  c++  java
  • 获取GridView中RowCommand的当前索引行(转)

    1. 获取GridView中RowCommand的当前索引行 前台添加一模版列,里面添加一个LinkButton
    2. 前台 (如果在后台代码中用e.CommandArgument取值的话前台代码就必须在按钮中设置CommandArgument的值,值为绑定的数据库字段
    3. <asp:TemplateField HeaderText="操作">
    4.     <ItemTemplate>
    5.         <asp:LinkButton ID="LinkButton1" runat="server" CommandName="QianRu" 
    6.         CommandArgument='<%# Eval("Id") %>'>签入</asp:LinkButton>  
    7.         <asp:LinkButton ID="LinkButton2" runat="server" CommandName="QianChu">签出</asp:LinkButton>
    8.     </ItemTemplate>
    9. </asp:TemplateField>
    10. 后台
    11. 在GridView里已经设置了LinkButton为事件处理按钮,将通过以下方法获取索引
    12. protected void gv_Company_RowCommand(object sender, GridViewCommandEventArgs e){
    13.         if (e.CommandName == "QianRu")
    14.     {     //取ID的值方法一   
    15.               GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值
    16.               inf id=Convert.ToInt32(GridView1.DataKeys[drv.RowIndex].Value); //此获取的值为GridView中绑定数据库中的主键值
    17.           //取ID的值方法二   
    18.               GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值
    19.               //此获取的值为GridView中绑定数据库中的主键值,取值方法是选中的行中的第一列的值,drv.RowIndex取得是选中行的索引
    20.           int id = Convert.ToInt32(GridView1.Rows[drv.RowIndex].Cells[0].Text); 
    21.           //取ID的值方法三  
    22.           //因为在客户端中就已经将LinkButton的CommandArgument与主键Id给绑定了所以在此可以直接用e.CommandArgument得出主键ID的值
    23.           int id = Convert.ToInt32(e.CommandArgument.ToString()); 
    24.         }
    25.     }
    26. 还有一种就是我们并不需要知道当前点击的是第几行,可以用以下方法实现要求: 
    27. <ItemTemplate> 
    28.       <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument=' <%# Eval("field1") %>' 
    29.       CommandName="play" Text=' <%# Eval("field2") %>'> </asp:LinkButton> 
    30. </ItemTemplate> 
    31. 上面这个LinkButton,Text绑定了字段2, CommandArgument绑定了字段1 
    32. 那么, 
    33. protected  void  GridView1_RowCommand(object  sender,  GridViewCommandEventArgs  e) 
    34.     if(e.CommandName="play")
    35.     {
    36.         LinkButton lb = (LinkButton)e.CommandSource; 
    37.         string  a  =  lb.Text;//这里可以获得点击行字段field2的值 
    38.         string b = e.CommandArgument;//这里可以获得点击行字段field1的值
    39.     }
    40. }
    41. 或:
    42. 如果是使用模板列,可以把数据的任意一列绑定到按钮的CommandArgument,如下: 
    43. <asp:TemplateField> 
    44. <ItemTemplate> 
    45. <asp:Button runat="server" CommandArgument='<%# Eval("id") %>' Text="Button" /> 
    46. </ItemTemplate> 
    47. </asp:TemplateField> 
    48. 一般可以绑定到主键列,这样可以在RowCommand通过e.CommandArgument获取当前行的主键,也便于进行其他操作 
    49. 如果是要获取行索引,比较麻烦一点,还是那个Button1,在GridView的RowDataBound事件中如下: 
    50. Button btn = (Button)e.Row.FindControl("Button1"); 
    51. if (btn != null
    52. btn.CommandArgument = e.Row.RowIndex.ToString(); 
    53. 这样就可以在RowCommand中通过 int rowId=Convert.ToInt32(e.CommandArgument.ToString()) 获取行索引了 

    转自:

    http://blog.csdn.net/sabty/article/details/4816160
  • 相关阅读:
    MR中简单实现自定义的输入输出格式
    简单实现CombineFileInputFormat
    提高mapreduce性能的七点建议
    MR中使用sequnceFIle输入文件
    Hive中使用LZO
    JVM启动参数详解 (转)
    ubuntu12.04中shell脚本无法使用source的原因及解决方法
    hadoop 错误
    poj 3211 Washing Clothes
    hdu 3535 AreYouBusy
  • 原文地址:https://www.cnblogs.com/hfzsjz/p/3726009.html
Copyright © 2011-2022 走看看