简体   繁体   中英

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

There are similar questions on Stackoverflow and other websites, but I seem to miss something.

I have a GridView bound to a DataTable which comes from a database. My goal is to update the one row at the time with a button which is in the same row which calls the following Method.

        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);
    }

It is being called from here:

    <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>

At my first try I had a OnRowCommand with the GridViewCommandEventArgs at the code behind.

But it still throws the same exception, although I've read on a couple of sites that changing it to OnRowEditing and GridViewEditEventArgs would solve the problem.

Thanks in advance,

Tim A

GridView's in ASP.NET have some built in commands - Deleted , Update , etc. What's happening is that your button has a command name of Update , and the Gridview is hijacking that from you and firing a RowUpdating event instead of your RowEditing event. Change your button command name to be Edit , and use a RowEditing event.

If you are using the Row_Command event to edit, then don't use the button command name "Update" for Update and "Delete" for Delete. Instead, use Edit or UP and Del respectively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM