【问题标题】:RowUpdating Event of Gridview is not working properlyGridview 的 RowUpdating 事件无法正常工作
【发布时间】:2013-05-15 11:47:13
【问题描述】:

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

为此,我做了以下工作:

Gridview>> EditColumns >> CommandField >> Edit,update,cancel 添加到网格中。

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")))` 

有空值。

在行更新事件中将异常作为“空引用异常”。

我的代码有什么问题?

我在哪里犯错。

请指导我。

【问题讨论】:

  • 你必须在两个事件上再次绑定gridview,即编辑和更新
  • 但没有绑定,效果应该在 DB 上。但它没有显示效果。正如我所说,它给了我例外
  • 您的更新命令也会更新表中的每一行。
  • @gzaxx 但我只设置了 1 个字段进行更新。
  • 我知道这不是您所要求的答案,但是下次使用实体框架并将其绑定到网格会更容易......更新方法将自动提交...

标签: c# asp.net gridview


【解决方案1】:
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
   gv.EditIndex = e.NewEditIndex;
   BindGridView();//You have  to  Bind GridView Again Here...
}

如果您在设置编辑索引后没有再次绑定 Gridview,则您的控件不受 gridview 的限制。这就是它给您空引用异常的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多