Gridview的RowUpdating事件无法正常工作

时间:2013-05-15 11:47:13

标签: c# asp.net gridview

我想通过gridview更新我的表格。 [Visual Studio 2010]

为此我做了以下事情:

的GridView>> EditColumns>> CommandField>>编辑,更新,取消添加到网格。

的GridView>> EditTemplates>>添加了文本框名称“TextBox1”>>结束编辑模板。

行编辑事件:

public int i;
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
   i=gv.EditIndex = e.NewEditIndex;
}

行更新事件:

protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                TextBox txtSymbol;

                txtSymbol = ((TextBox)(gv.Rows[e.RowIndex].Cells[3].FindControl("TextBox1")));

                con.Open();
                cmd = new SqlCommand("update temp set Symbol=@Symbol", con);
                cmd.Parameters.AddwithValue("@Symbol",txtSymbol.Text);
                cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
            }
        }

我从here提到了这段代码。

这里提到的第一行应该是这样的:

txtSymbol =((TextBox)(gv.Rows [e.RowIndex] .Cells [3] .FindControl(“TextBox1”)));

所有这些编码的东西都不起作用。

`((TextBox)(gv.Rows[e.RowIndex].Cells[3].FindControl("TextBox1")))` 

具有空值。

行更新事件中的异常为“Null Referance Exception。”

我的代码有什么问题?

我犯错的地方。

请指导我。

1 个答案:

答案 0 :(得分:1)

protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
   gv.EditIndex = e.NewEditIndex;
   BindGridView();//You have  to  Bind GridView Again Here...
}

如果你不再绑定Gridview在设置它之后编辑索引你的控件没有限制使用gridview.Thats为什么它给你nullreferenceexception。

相关问题