【问题标题】:The GridView ' ' fired event RowUpdating which wasn't handled. C# code behind asp.netGridView ' ' 触发了未处理的事件 RowUpdating。 asp.net 后面的 C# 代码
【发布时间】:2017-04-18 20:15:46
【问题描述】:

Stackoverflow 和其他网站上也有类似的问题,但我好像漏掉了什么。

我有一个绑定到来自数据库的 DataTable 的 GridView。我的目标是使用同一行中调用以下方法的按钮一次更新一行。

        protected void TestsToBeDone_RowUpdating(object sender, GridViewEditEventArgs e)
    {
        SqlConnection myConnection = getConnection();
        myConnection.Open();

        int index = Convert.ToInt32(e.NewEditIndex);

        GridViewRow selectedRow = TestsToBeDone.Rows[index];
        TableCell LABAVIDc = selectedRow.Cells[1];
        int LABAVID = Convert.ToInt32(LABAVIDc.Text);

        TableCell assistentc = selectedRow.Cells[5];

        string query = "UPDATE Labaanvraag " +
                       "SET Status='4' "+
                                 "WHERE LABAVID ='"+LABAVID+"'";
        }

        SqlCommand commandZ = new SqlCommand(query, myConnection);
        commandZ.ExecuteNonQuery();            

        myConnection.Close();

        SetPatientTestGrid(Session["PATID"], e);
    }

从这里调用它:

    <asp:GridView ID="TestsToBeDone" runat="server" EnableModelValidation="True" OnRowEditing="TestsToBeDone_RowUpdating">
    <Columns>
        <asp:ButtonField ButtonType="Button" CommandName="Update" 
            HeaderText="Afgenomen" ShowHeader="True" Text="Afgenomen" />
    </Columns>
</asp:GridView>

在我第一次尝试时,我在后面的代码中有一个带有 GridViewCommandEventArgs 的 OnRowCommand。

但它仍然会引发相同的异常,尽管我在几个网站上阅读过将其更改为 OnRowEditing 和 GridViewEditEventArgs 可以解决问题。

提前致谢,

蒂姆·A

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    ASP.NET 中的 GridView 有一些内置命令 - DeletedUpdate 等。发生的情况是您的按钮的命令名称为 Update,而 Gridview 正在从您那里劫持它并触发RowUpdating 事件而不是您的 RowEditing 事件。将您的按钮命令名称更改为Edit,并使用RowEditing 事件。

    【讨论】:

    • 我试过了。但是现在按钮不再调用更新方法了。但它也不会抛出异常。
    • 啊,明白了。你对这个地方的看法是对的,但我把它改成了edit,现在它可以工作了:) 感谢你为我指明了正确的方向!如果您更改答案,我将设置为答案:)
    • 抱歉回复晚了,但我的意思是 CommandName="Edit" :)
    • 优秀的答案。我应该说一万谢谢你。
    【解决方案2】:

    如果您使用Row_Command 事件进行编辑,请不要将按钮命令名称“更新”用于更新,将“删除”用于删除。相反,请分别使用 Edit 或 UP 和 Del。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 2013-01-21
      相关资源
      最近更新 更多