菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
236
0

asp.net gridview的Rowcommand命令中获取行索引的方法总结

原创
05/13 14:22
阅读数 13544









二、通过在RowDataBound事件中把行索引绑定到控件的CommandArgument

http://www.cppcns.com



复制代码 代码如下:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex = -1;
GridViewRow row = null;
switch (e.CommandName) ...{
case "Command1": // 模板列
// 对于模板列内的按钮,我们需要显示绑定行索引到按钮的 CommandArgument 属性
// 以获取触发事件的行信息
rowIndex = Convert.ToIntNlWza32(e.CommandArgument);
row = GridView1.Rows[rowIndex];
DisplayInfo(row, e.CommandName);
// your codes
//
break;
case "Command2": // 模板列
// 同样处于模板列中,但不采用 Command1 方式,而是通过 NamingContrainer 属性
// 直接获取当前的 GridViewRow
Control cmdControl = e.CommandSource as Control; // 表示触发事件的 IButtonControl,保持统一性并便于后续操作,我们这里直接转化为控件基类 Control
row = cmdControl.NamingContainer as GridViewRow;
DisplayInfo(row, e.CommandName);
// your codes
//
break;
case 编程客栈"Command3": // 绑定列
// 对于 ButtonField 列,数据源控件内部自动以适当的项索引值填充 CommandArgument 属性。
// 而无需我们显示绑定其 CommandArgument 属性
// 注意,我们这里无法采用 Command2 的方式,对于 BUttonField 触发的事件,
// GridViewCommandEventArgs.CommandSource 表示的包含此按钮的 GridView
rowIndex = Convert.ToInt32(e.CommandArgument);
row = GridView1.Rows[rowIndex];
DisplayInfo(row, e.CommanNlWzadName);
// your codes
//
break;
} http://www.cppcns.com
}
本文标题: asp.net gridview的Rowcommand命令中获取行索引的方法总结
本文地址: http://www.cppcns.com/wangluo/aspnet/57245.html

发表评论

0/200
236 点赞
0 评论
收藏
为你推荐 换一批