获取GridView中RowCommand的当前索引行(转)
- 获取GridView中RowCommand的当前索引行 前台添加一模版列,里面添加一个LinkButton
- 前台 (如果在后台代码中用e.CommandArgument取值的话前台代码就必须在按钮中设置CommandArgument的值,值为绑定的数据库字段
- <asp:TemplateField HeaderText="操作">
- <ItemTemplate>
- <asp:LinkButton ID="LinkButton1" runat="server" CommandName="QianRu"
- CommandArgument='<%# Eval("Id") %>'>签入</asp:LinkButton>
- <asp:LinkButton ID="LinkButton2" runat="server" CommandName="QianChu">签出</asp:LinkButton>
- </ItemTemplate>
- </asp:TemplateField>
- 后台
- 在GridView里已经设置了LinkButton为事件处理按钮,将通过以下方法获取索引
- protected void gv_Company_RowCommand(object sender, GridViewCommandEventArgs e){
- if (e.CommandName == "QianRu")
- { //取ID的值方法一
- GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值
- inf id=Convert.ToInt32(GridView1.DataKeys[drv.RowIndex].Value); //此获取的值为GridView中绑定数据库中的主键值
- //取ID的值方法二
- GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值
- //此获取的值为GridView中绑定数据库中的主键值,取值方法是选中的行中的第一列的值,drv.RowIndex取得是选中行的索引
- int id = Convert.ToInt32(GridView1.Rows[drv.RowIndex].Cells[0].Text);
- //取ID的值方法三
- //因为在客户端中就已经将LinkButton的CommandArgument与主键Id给绑定了所以在此可以直接用e.CommandArgument得出主键ID的值
- int id = Convert.ToInt32(e.CommandArgument.ToString());
- }
- }
- 还有一种就是我们并不需要知道当前点击的是第几行,可以用以下方法实现要求:
- <ItemTemplate>
- <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument=' <%# Eval("field1") %>'
- CommandName="play" Text=' <%# Eval("field2") %>'> </asp:LinkButton>
- </ItemTemplate>
- 上面这个LinkButton,Text绑定了字段2, CommandArgument绑定了字段1
- 那么,
- protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
- {
- if(e.CommandName="play")
- {
- LinkButton lb = (LinkButton)e.CommandSource;
- string a = lb.Text;//这里可以获得点击行字段field2的值
- string b = e.CommandArgument;//这里可以获得点击行字段field1的值
- }
- }
- 或:
- 如果是使用模板列,可以把数据的任意一列绑定到按钮的CommandArgument,如下:
- <asp:TemplateField>
- <ItemTemplate>
- <asp:Button runat="server" CommandArgument='<%# Eval("id") %>' Text="Button" />
- </ItemTemplate>
- </asp:TemplateField>
- 一般可以绑定到主键列,这样可以在RowCommand通过e.CommandArgument获取当前行的主键,也便于进行其他操作
- 如果是要获取行索引,比较麻烦一点,还是那个Button1,在GridView的RowDataBound事件中如下:
- Button btn = (Button)e.Row.FindControl("Button1");
- if (btn != null)
- {
- btn.CommandArgument = e.Row.RowIndex.ToString();
- }
- 这样就可以在RowCommand中通过 int rowId=Convert.ToInt32(e.CommandArgument.ToString()) 获取行索引了
转自:
获取GridView中RowCommand的当前索引行(转)的更多相关文章
- 转:获取GridView中RowCommand的当前索引行
获取GridView中RowCommand的当前索引行 前台添加一模版列,里面添加一个LinkButton 前台 (如果在后台代码中用e.CommandArgument取值的话前台代码就必须在按钮中设 ...
- GridView中实现点击某行的任意位置就选中该行
来源:http://auv2009.blog.163.com/blog/static/68858712200992731010670/ 在 GridView中增加一列:(该列是选择按钮,让其不显示) ...
- Js获取Gridview中Dropdownlist选中状态
在Gridview中加入Dropdownlist模板列,加入DropDownlist 是一种常用的操作,其中涉及到如何获取选择项和Gridview重新绑定两个要点. 如图 前台代码如下 <%@ ...
- 获取GRIDVIEW中的TemplateField显示的文本值
GRIDVIEW中数据源绑定后的属性绑定我一般采取2种办法 一个是BoundField,只要设置DataField的对应属性名即可: 如: <asp:BoundField HeaderText ...
- AutoIt获取Gridview中可以修改列的值
有一个界面如上图:黑色框框部分是一个整体,也是一个gridview,如果我想把框框中右侧数据获取出来,该如何操作? 我尝试过了很多途径,都无法成功. 今天,我发现,当鼠标焦点在黑色框框左侧的部分的时候 ...
- 【381】python 获取列表中重复元素的索引值
参考:获取python的list中含有重复值的index方法_python_脚本之家 核心思想:建立字典,遍历列表,把列表中每个元素和其索引添加到字典里面 cc = [1, 2, 3, 2, 4] f ...
- c#获取数组中指定元素的索引
//获取元素的索引 ArrayList arrList = new ArrayList(); ; i < array.Length; i++) { ) { arrList.Add(i); } } ...
- python之enumerate函数:获取列表中每个元素的索引和值
源码举例: def enumerate_fn(): ''' enumerate函数:获取每个元素的索引和值 :return:打印每个元素的索引和值 ''' list = ['] for index, ...
- Python获取list中指定元素的索引
在平时开发过程中,经常遇到需要在数据中获取特定的元素的信息,如到达目的地最近的车站,橱窗里面最贵的物品等等.怎么办?看下面 方法一: 利用数组自身的特性 list.index(target), 其中a ...
随机推荐
- new、delete、以及queue类
本来以为很容易的,结果还是写了我两个小时. 用指针模拟queue类,再加上类,各种错误,总算是解决掉了-- #include<iostream> #include<cstdlib&g ...
- Android 自动化测试
Python +Android +uiautomator test 在init中定义的方法 uiautomator 该模块是android的一个python包装uiautomator测试框架 ...
- Oracle用户密码过期的处理方法
受影响版本:Oracle11g以上版本. 导致密码消失的原因:Oracle 11g中默认的DEFAULT概要文件中口令有效期PASSWORD_LIFE_TIME默认值为180天. 当以客户端登 ...
- 为python 添加新功能-dump
一直觉得thinkphp提供的dump函数挺好用的,但是python里面没有,就一直想着写个简单的. dir是我比较常用的一个内置函数了,但是显示效果实在有点受不了,每次我都要从大量的字符串里找到我需 ...
- HashTable浅析
本文转载自: http://rock3.info/blog/2013/12/05/hashtable%E6%B5%85%E6%9E%90/ 一.Hash特点 Hash,就是杂凑算法,Hash(str1 ...
- [Linux实用工具]Linux监控工具munin的展示(Nginx)
Munin的安装和配置可以参考第一篇文章: [Linux实用工具]Linux监控工具munin的安装和配置 http://www.cnblogs.com/rond/p/3757804.html Mun ...
- IDEA无法启动debugger,报错Address localhost:1099 is already in use
Address localhost:1099 is already in use http://blog.csdn.net/huazhongkejidaxuezpp/article/details/4 ...
- Java API学习(一) ArrayList源码学习
ArrayList在平常用的还挺多的,用起来十分舒服,顺手.这里来学习一下它的源码. 类定义 下面是类的定义: public class ArrayList<E> extends Abst ...
- TP v5中环境变量在项目中的应用
环境变量,顾名思义就是在不同的系统环境,同一个变量的值可以有所不同. 如开发环境.测试环境与正式环境下,数据库配置.静态资源文件Url前缀.缓存.各种key等配置都不相同,对于提交到仓库中的代码,理论 ...
- 输//ip提示找不到应用程序
输//ip提示找不到应用程序??? (未测试)试试:环境变量的 PATH 中添加 C:\Windows\system32 (未测试)试试:默认程序里----协议关联里:管理ie