首页 > 解决方案 > 用户未处理参数超出范围异常

问题描述

以下代码在单击编辑按钮时生成上述错误。

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]

标签: c#

解决方案


您可以使用控件类而不是它们的类型:

GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);

推荐阅读