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. {
  35. if(e.CommandName="play")
  36. {
  37. LinkButton lb = (LinkButton)e.CommandSource;
  38. string  a  =  lb.Text;//这里可以获得点击行字段field2的值
  39. string b = e.CommandArgument;//这里可以获得点击行字段field1的值
  40. }
  41. }
  42. 或:
  43. 如果是使用模板列,可以把数据的任意一列绑定到按钮的CommandArgument,如下:
  44. <asp:TemplateField>
  45. <ItemTemplate>
  46. <asp:Button runat="server" CommandArgument='<%# Eval("id") %>' Text="Button" />
  47. </ItemTemplate>
  48. </asp:TemplateField>
  49. 一般可以绑定到主键列,这样可以在RowCommand通过e.CommandArgument获取当前行的主键,也便于进行其他操作
  50. 如果是要获取行索引,比较麻烦一点,还是那个Button1,在GridView的RowDataBound事件中如下:
  51. Button btn = (Button)e.Row.FindControl("Button1");
  52. if (btn != null)
  53. {
  54. btn.CommandArgument = e.Row.RowIndex.ToString();
  55. }
  56. 这样就可以在RowCommand中通过 int rowId=Convert.ToInt32(e.CommandArgument.ToString()) 获取行索引了

转:获取GridView中RowCommand的当前索引行的更多相关文章

  1. 获取GridView中RowCommand的当前索引行(转)

    获取GridView中RowCommand的当前索引行 前台添加一模版列,里面添加一个LinkButton 前台 (如果在后台代码中用e.CommandArgument取值的话前台代码就必须在按钮中设 ...

  2. Js获取Gridview中Dropdownlist选中状态

    在Gridview中加入Dropdownlist模板列,加入DropDownlist 是一种常用的操作,其中涉及到如何获取选择项和Gridview重新绑定两个要点. 如图 前台代码如下 <%@ ...

  3. 获取GRIDVIEW中的TemplateField显示的文本值

    GRIDVIEW中数据源绑定后的属性绑定我一般采取2种办法 一个是BoundField,只要设置DataField的对应属性名即可: 如: <asp:BoundField HeaderText ...

  4. AutoIt获取Gridview中可以修改列的值

    有一个界面如上图:黑色框框部分是一个整体,也是一个gridview,如果我想把框框中右侧数据获取出来,该如何操作? 我尝试过了很多途径,都无法成功. 今天,我发现,当鼠标焦点在黑色框框左侧的部分的时候 ...

  5. GridView中实现点击某行的任意位置就选中该行

    来源:http://auv2009.blog.163.com/blog/static/68858712200992731010670/ 在 GridView中增加一列:(该列是选择按钮,让其不显示) ...

  6. 【381】python 获取列表中重复元素的索引值

    参考:获取python的list中含有重复值的index方法_python_脚本之家 核心思想:建立字典,遍历列表,把列表中每个元素和其索引添加到字典里面 cc = [1, 2, 3, 2, 4] f ...

  7. c#获取数组中指定元素的索引

    //获取元素的索引 ArrayList arrList = new ArrayList(); ; i < array.Length; i++) { ) { arrList.Add(i); } } ...

  8. python之enumerate函数:获取列表中每个元素的索引和值

    源码举例: def enumerate_fn(): ''' enumerate函数:获取每个元素的索引和值 :return:打印每个元素的索引和值 ''' list = ['] for index, ...

  9. Python获取list中指定元素的索引

    在平时开发过程中,经常遇到需要在数据中获取特定的元素的信息,如到达目的地最近的车站,橱窗里面最贵的物品等等.怎么办?看下面 方法一: 利用数组自身的特性 list.index(target), 其中a ...

随机推荐

  1. html页面和jsp页面的区别

    html页面: html页面是静态页面,可以使用html+css+js实现页面的各种效果,单纯使用html布局出来的页面是设定好的页面,可以使用本地浏览器打开.同时搭配使用ajax实现数据交互效果的页 ...

  2. Easyui validatebox后台服务端验证

    Easyui validatebox的验证提示十分好用,可是在实际项目的运用中,经常会遇到需要服务器验证后并返回验证结果信息,比如验证用户名.手机号.邮箱是否已存在.于是就想着怎么拓展Easyui的验 ...

  3. SharePoint 2013 安装配置(3-2)

    第三部分SQL Server后端配置篇幅较长,上一篇介绍SQL Server 前提条件及安装(3-1),这篇分享SQL Server功能安装配置,请参考以下步骤. 10.在“安装角色”屏幕上,选择“S ...

  4. Python + selenium之组织unittest单元测试用例

    当增加被测功能和相应的测试用例之后unittest单元测试框架如何扩展和组织新增的测试用例的. # coding =utf-8 # calculator class Count (): def __i ...

  5. HTML页面右键事件

    <script type="text/javascript"> <!-- document.onmousedown = function (e) { var e ...

  6. Array - Remove Element

    /** * 无额外空间.顺序可以被改变.不需要修改后面的数字. * @param nums 数组 * @param val 目标值 * @return nums中移除val后的长度 */ public ...

  7. 在DataGridView控件中验证数据输入

    实现效果: 知识运用: DataGridView控件的公共事件CellValidating //将System.Windows.Forms.DataGridViewCellValidatingEven ...

  8. stixel-world跑在kitti数据集

    kitti数据集中每一帧的Calibration不同,每一帧都存储了4个相机的Calibration http://ww.cvlibs.net/publications/Geiger2013IJRR. ...

  9. PAT (Basic Level) Practise (中文)- 1010. 一元多项式求导 (25)

    http://www.patest.cn/contests/pat-b-practise/1010 设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为n*xn-1.) 输入格式:以指数递降 ...

  10. PMD 编译 语法分析 词法分析 抽象语法树

    编译原理 163 课堂 http://mooc.study.163.com/learn/-1000002001?tid=1000003000#/learn/content?type=detail&am ...