1

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 可以解决问题。

提前致谢,

蒂姆一个

4

2 回答 2

5

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

于 2012-10-22T17:27:20.117 回答
0

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

于 2013-05-26T16:08:52.900 回答