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()) 获取行索引了

转自:

http://blog.csdn.net/sabty/article/details/4816160

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

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

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

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

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

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

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

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

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

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

    有一个界面如上图:黑色框框部分是一个整体,也是一个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. new、delete、以及queue类

    本来以为很容易的,结果还是写了我两个小时. 用指针模拟queue类,再加上类,各种错误,总算是解决掉了-- #include<iostream> #include<cstdlib&g ...

  2. Android 自动化测试

    Python +Android +uiautomator test  在init中定义的方法 uiautomator     该模块是android的一个python包装uiautomator测试框架 ...

  3. Oracle用户密码过期的处理方法

    受影响版本:Oracle11g以上版本.   导致密码消失的原因:Oracle 11g中默认的DEFAULT概要文件中口令有效期PASSWORD_LIFE_TIME默认值为180天.   当以客户端登 ...

  4. 为python 添加新功能-dump

    一直觉得thinkphp提供的dump函数挺好用的,但是python里面没有,就一直想着写个简单的. dir是我比较常用的一个内置函数了,但是显示效果实在有点受不了,每次我都要从大量的字符串里找到我需 ...

  5. HashTable浅析

    本文转载自: http://rock3.info/blog/2013/12/05/hashtable%E6%B5%85%E6%9E%90/ 一.Hash特点 Hash,就是杂凑算法,Hash(str1 ...

  6. [Linux实用工具]Linux监控工具munin的展示(Nginx)

    Munin的安装和配置可以参考第一篇文章: [Linux实用工具]Linux监控工具munin的安装和配置 http://www.cnblogs.com/rond/p/3757804.html Mun ...

  7. IDEA无法启动debugger,报错Address localhost:1099 is already in use

    Address localhost:1099 is already in use http://blog.csdn.net/huazhongkejidaxuezpp/article/details/4 ...

  8. Java API学习(一) ArrayList源码学习

    ArrayList在平常用的还挺多的,用起来十分舒服,顺手.这里来学习一下它的源码. 类定义 下面是类的定义: public class ArrayList<E> extends Abst ...

  9. TP v5中环境变量在项目中的应用

    环境变量,顾名思义就是在不同的系统环境,同一个变量的值可以有所不同. 如开发环境.测试环境与正式环境下,数据库配置.静态资源文件Url前缀.缓存.各种key等配置都不相同,对于提交到仓库中的代码,理论 ...

  10. 输//ip提示找不到应用程序

    输//ip提示找不到应用程序??? (未测试)试试:环境变量的 PATH 中添加 C:\Windows\system32 (未测试)试试:默认程序里----协议关联里:管理ie