转:获取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取值的话前台代码就必须在按钮中设 ...
- Js获取Gridview中Dropdownlist选中状态
在Gridview中加入Dropdownlist模板列,加入DropDownlist 是一种常用的操作,其中涉及到如何获取选择项和Gridview重新绑定两个要点. 如图 前台代码如下 <%@ ...
- 获取GRIDVIEW中的TemplateField显示的文本值
GRIDVIEW中数据源绑定后的属性绑定我一般采取2种办法 一个是BoundField,只要设置DataField的对应属性名即可: 如: <asp:BoundField HeaderText ...
- AutoIt获取Gridview中可以修改列的值
有一个界面如上图:黑色框框部分是一个整体,也是一个gridview,如果我想把框框中右侧数据获取出来,该如何操作? 我尝试过了很多途径,都无法成功. 今天,我发现,当鼠标焦点在黑色框框左侧的部分的时候 ...
- GridView中实现点击某行的任意位置就选中该行
来源:http://auv2009.blog.163.com/blog/static/68858712200992731010670/ 在 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 ...
随机推荐
- 去除inline-block间隙的几种方法
为什么会产生间隙? 由于编写代码时的美观和可读性,在代码中添加回车或空格而产生的间隙. html代码: <ul class="container"> <li> ...
- Ubuntu获取root 权限,开机自动登入root
新机器获取root权限,只需要给root 增加密码: sudo passwd root 修改开机自动登入: #sudo gedit /etc/lightdm/lightdm.conf 修改参数: au ...
- Jquery 如何获取表单中的全部元素的值
1.使用var formData = $(formId).serialize()获取:获取数据的格式为url参数形式的字符串.例如:id=100&name=张三 2.服务器端使用parse ...
- 如何删除 CentOS 6 更新后产生的多余的内核?
第一种方法:通过命令的方式解决多余的内核 1.首先查看当前内核的版本号: [root@jxatei ~]# uname -a Linux jxatei.server2.6.32-573.1.1.el ...
- python3发送邮件01(简单例子,不带附件)
# -*- coding:utf-8 -*-import smtplibfrom email.header import Headerfrom email.mime.text import MIMET ...
- PSNR
PSNR,峰值信噪比,通常用来评价一幅图像压缩后和原图像相比质量的好坏,当然,压缩后图像一定会比原图像质量差的,所以就用这样一个评价指标来规定标准了.PSNR越高,压缩后失真越小.这里主要定义了两个值 ...
- targetcli save error
iscsi configuration unable to save python error “ValueError: 'Implict and Explict' is not in list” / ...
- 安装JDK1.8以及配置环境变量的步骤
一. 首先到官网下载jdk1.8,下载的版本分为windows和linux,这里需要安装操作系统进行下载.我的是64位就下载x64,32位系统则下载x86 二. 然后就是安装,双击进行安装,这里不用更 ...
- NPM下载模块包说明
博主对npm包安装收集了各种资料和实践后对它们之间的差异整理,写下这篇文章避免自己忘记,同时也给node.js猿友一点指引. 我们在使用 npm install 安装模块的模块的时候 ,一般会使用下面 ...
- Python——字典dict()详解
一.字典 字典是Python提供的一种数据类型,用于存放有映射关系的数据,字典相当于两组数据,其中一组是key,是关键数据(程序对字典的操作都是基于key),另一组数据是value,可以通过key来进 ...