c# - 用户未处理参数超出范围异常
问题描述
以下代码在单击编辑按钮时生成上述错误。
protected void uxStaffGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
//Determine the RowIndex of the Row whose Button was clicked.
int rowIndex = Convert.ToInt32(e.CommandArgument);
//Reference the GridView Row.
GridViewRow row = uxStaffGrid.Rows[rowIndex];
//Fetch value of Name.
// string name = (row.FindControl("txtId") as Label).Text;
string Id = row.Cells[1].Text;
ClientScript.RegisterStartupScript(this.GetType(), "ConsumeID", "ConsumeID('" + Id + "');", true);
}
}
错误在线发生:
GridViewRow row = uxStaffGrid.Rows[rowIndex]
解决方案
您可以使用控件类而不是它们的类型:
GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
推荐阅读
- reactjs - React js - 仅使用一种状态而不是多种状态来处理“event.target.value”
- swift - 渐变作为 SwiftUI 中文本的前景色
- python - 正则表达式中匹配pattern_1但不匹配pattern_2的模式?
- python - 打印不符合数据间隔设置
- visual-studio-code - Visual Studio Code:更改调试面板的字体大小和字体系列以及调试控制台输入
- string - 在 Matlab 中打开文本文件并替换两个字符串
- bluetooth - 减少 bluetoothctl 冗长
- c# - 我的 lambda 函数没有返回错误的请求
- javascript - 摩纳哥编辑器:只显示文件的一部分
- c# - 在 c# webforms 中,使用端口获取当前路径不返回端口