【转】通过在RowDataBound事件中把行索引绑定到控件的CommandArgument,然后在RowCommand事件中取出
2010-12-07 01:00
344 查看
代码
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;
}
}
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;
}
}
相关文章推荐
- 任意控件的事件绑定到Command
- ItemCommand,ItemDataBound事件中获取已绑定行的字段的值和服务器端控件
- GridView控件CommandArgument 绑定多个参数
- 安卓中通过循环动态添加了一堆自定义的控件,怎么给这些控件绑定事件?
- 通过Attached Property给控件绑定Command(二)
- 日积(Running)月累(ZSSURE):WCF学习之“通过事件绑定控制WinForm宿主程序主界面控件”
- GridView绑定时通过RowDataBound事件获取数据源列值
- GridView绑定时通过RowDataBound事件获取数据源列值
- Repeater 的操作,数据的显示,绑定,动态取控件中的值和Repeater1_ItemCommand事件的相关用法
- jQuery+ajax从数据库取值和和绑定前台html控件上,然后再通过ajax把值传到后台,让后台写入到数据库
- WPF MVVM后台绑定没有Command属性的控件,绑定任何事件事件
- 通过Attached Property给控件绑定Command(一)
- Repeater控件通过列索引绑定数据
- gridview绑定checkbox控件后,通过单击CheckedChanged事件获取当前选择行的数据
- Repeater 的操作,数据的显示,绑定,动态取控件中的值和Repeater1_ItemCommand事件的相关用法
- 11.17日,GridView 的RowCommand事件中,e.CommandArgument不是行号,那么要取这一行的某一列怎么办
- GridView绑定时通过RowDataBound事件获取数据源列值
- .NET数据控件 e.commandargument()绑定多个值
- GridView控件CommandArgument 绑定多个参数
- 通过Attached Property给控件绑定Command(三)