0

当我单击更新按钮时,我收到此错误,我的更新单击事件

protected void btnUpdate_Click(object sender, CommandEventArgs e)
{
    int idx = Convert.ToInt32(e.CommandArgument);
    GridViewRow gr = gvResTasks.Rows[idx];
    ...
}

当 idx 值高达 19 时,它工作正常..当它大于 19 时,这个错误来了..你能帮我解决这个问题吗

更新按钮.aspx

<ItemTemplate> <asp:Button runat="server" ID="btnUpdate" Text="Update" OnCommand="btnUpdate_Click" CommandArgument="<%# Container.DataItemIndex %>" /> <%--<asp:Button runat="server" ID="Button1" Text="Update" onclick="btnUpdate_Click" />--%> </ItemTemplate>    

更新按钮.aspx.cs

protected void btnUpdate_Click(object sender, CommandEventArgs e)
{
   // int idx = Convert.ToInt32(e.CommandArgument);
    int idx = Convert.ToInt32(e.CommandArgument);
    GridViewRow gr = gvResTasks.Rows[idx];

    Label hf = (Label)gr.FindControl("hdEmployeeID");

    string sEmpID = hf.Text;
    lblEmployeeID.Text = sEmpID;
    lblEmployeeID2.Text = sEmpID;

    int Rows = gvResTasks.Rows.Count;
    for (int x = 0; x < Rows; x++)
    {
        GridViewRow tr = gvResTasks.Rows[x];
        tr.CssClass = "WhiteBack";
    }

    ClearMessages();

    gr.CssClass = "TealBack";

    string sSubTaskID = gr.Cells[0].Text;
    lblSubTaskID.Text = sSubTaskID;

    lblSubTaskName.Text = @gr.Cells[1].Text;

    LoadSubTaskInfo();

    Single sProgress = 0;
    Boolean bok = Single.TryParse(lblActualProgress.Text, out sProgress);




    DateTime dPF = DateTime.Parse(lblPlannedFinish.Text);

    if (dPF > DateTime.Now)
    {
        btnFinish.Enabled = false;
    }
    else if (dPF <= DateTime.Now)
    {
        if (sProgress >= 100)
        {
            btnFinish.Enabled = false;
        }
        else
        {
            btnFinish.Enabled = true;
        }
        //btnFinish.Enabled = true;
    }

    panelUpdate.CssClass = "float";
    panelUpdate.Visible = false;
    if (gr.Cells[7].Text != "True")
    {
        panelUpdate.Visible = true;
    }


}
4

2 回答 2

1

无需使用e.CommandArgument查找GridViewRow. 就这样做

GridViewRow gr = ((Button)sender).NamingContainer as GridViewRow;
于 2012-10-04T04:56:17.387 回答
0

你有多少行?

试试这个:

int idx = Convert.ToInt32(e.CommandArgument);

if (idx < gvResTasks.Rows.Count)
{
    GridViewRow gr = gvResTasks.Rows[idx];
}

另外,你能提供 btnUpdate ASPX 代码吗?

于 2012-10-04T02:52:55.980 回答