簡體   English   中英

GridView''觸發了未處理的事件RowUpdating。 ASP.NET背后的C#代碼

[英]The GridView ' ' fired event RowUpdating which wasn't handled. C# code behind asp.net

在Stackoverflow和其他網站上也有類似的問題,但我似乎錯過了一些東西。

我有一個GridView綁定到來自數據庫的DataTable。 我的目標是同時使用同一行中的按鈕來更新一行,該按鈕將調用以下方法。

        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

ASP.NET中的GridView具有一些內置命令DeletedUpdate等。發生的是您的按鈕的命令名稱為Update ,而Gridview劫持了您的該命令並觸發了RowUpdating事件而不是RowEditing事件。 將按鈕命令名稱更改為Edit ,並使用RowEditing事件。

如果要使用Row_Command事件進行編輯, Row_Command按鈕命令名“ Update”用於更新,而將“ Delete”用於刪除。 而是分別使用Edit或UP和Del。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM