11
votes

I’m getting this error over and over again.

Loading the data into the GridView works, but when I want to delete a row I'm getting that error.

<asp:GridView ID="OrdersGridView" runat="server" AutoGenerateColumns="False" onrowdeleted="OrdersGridView_RowDeleted">
    <Columns>
        <asp:TemplateField HeaderText="Product Name">
            <ItemTemplate>
                <asp:HiddenField runat="server" ID="HiddenField1" Value='<%#Eval("oid")%>'></asp:HiddenField>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="titel" HeaderText="Name" />
        <asp:BoundField DataField="oid" HeaderText="Itemno" />
        <asp:BoundField DataField="prijs" HeaderText="Price" />
        <asp:CommandField ButtonType="Link" CausesValidation="false" HeaderText="Update" ShowDeleteButton="True" />
        <asp:BoundField DataField="prijs" HeaderText="Subtotal" />
    </Columns>
</asp:GridView>

C# codebehind - I'm not really deleting the row from the database but it's a test:

protected void OrdersGridView_RowDeleted(object sender, System.Web.UI.WebControls.GridViewDeletedEventArgs e)
{
    if (e.Exception != null)
    {
        lblStatus.Text = e.Exception.ToString();
    }
    else 
    {
        string sValue = ((HiddenField)OrdersGridView.SelectedRow.Cells[1].FindControl("HiddenField1")).Value;
        lblStatus.Text = sValue;
    }
}

But after clicking, I get a bigass yellow page with the next error:

The GridView 'OrdersGridView' fired event RowDeleting which wasn't handled.

3

3 Answers

12
votes

Having a Delete button, or even a regular button in a GridView with a CommandName of delete, will automatically try to fire OnRowDeleting. You can just add it in to make things happy, but don't have it do anything so it doesn't affect the behavior of your delete.

You could add OnRowDeleting to your GridView:

<asp:GridView ID="OrdersGridView" runat="server" AutoGenerateColumns="False" onrowdeleted="OrdersGridView_RowDeleted" OnRowDeleting="OrdersGridView_RowDeleting">

And then in your CodeBehind add:

void OrdersGridView_RowDeleting (object sender, GridViewDeleteEventArgs e)
{
}
6
votes

change your row command name from delete to any other like deleterecord

0
votes

It looks like you are handling the "onrowdeleted" event, not the "RowDeleting" event

in your markup, change: onrowdeleted="OrdersGridView_RowDeleted"

to RowDeleting="OrdersGridView_RowDeleting"

Look a the docs for this event: you will also see that your handler's signature will need to change: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.onrowdeleting.aspx your new handler will look someting like this:

 protected void OrdersGridView_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e) { 
 if (e.Exception != null) { 
    lblStatus.Text = e.Exception.ToString(); 
   } 
   else 
   {
    string sValue = ((HiddenField)OrdersGridView.SelectedRow.Cells[1].FindControl("HiddenField1")).Value; lblStatus.Text = sValue; 
    } 
 }

the RowDeleting event happens, then the onrowdeleted event. The RowDeleting probally lets you Cancel the event.