最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:动视网 责编:小采 时间:2020-11-27 22:43:15
文档

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

asp.net gridview的Rowcommand命令中获取行索引的方法总结:一、通过命令源获取当前行索引。 方法比较多, GridView 的 Command 事件中无法象 DataGrid 那样直接获取行, 法1, GridViewRow drv = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));//CommandSour
推荐度:
导读asp.net gridview的Rowcommand命令中获取行索引的方法总结:一、通过命令源获取当前行索引。 方法比较多, GridView 的 Command 事件中无法象 DataGrid 那样直接获取行, 法1, GridViewRow drv = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));//CommandSour


一、通过命令源获取当前行索引。

方法比较多, GridView 的 Command 事件中无法象 DataGrid 那样直接获取行,

法1,
GridViewRow drv = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));//CommandSource 引起事件的命令源,(疑问,根据MSDN说的是GridView,如果这样的话这样操作是错误的,但我得到的确实正确的,那说明得到的是BUtton控件,等待以后查证).
drv.RowIndex


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

由于事件参数 GridViewCommandEventArgs 并不公开Row属性指示当前行,(DataGridCommandEventArgs 公开 Item 属性以获取当然 DataGridItem,不知 ASP.NET Team 是如何考虑这一设计的),因此需要一点“技巧”来获取此属性。

其实这是一个早就已知的问题,鉴于CSDN里面每每有人疑惑,这里稍微整理下,便于参阅:

代码如下:


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex = -1;
GridViewRow row = null;
switch (e.CommandName) ...{
case "Command1": // 模板列
// 对于模板列内的按钮,我们需要显示绑定行索引到按钮的 CommandArgument 属性
// 以获取触发事件的行信息
rowIndex = Convert.ToInt32(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.CommandName);
// your codes
//
break;
}
}

文档

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

asp.net gridview的Rowcommand命令中获取行索引的方法总结:一、通过命令源获取当前行索引。 方法比较多, GridView 的 Command 事件中无法象 DataGrid 那样直接获取行, 法1, GridViewRow drv = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));//CommandSour
推荐度:
标签: 获取 命令 row
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top