C# · 12月 30, 2021

c# – GridView’OrdersGridView’触发的事件RowDeleting没有被处理

我一遍又一遍地收到这个错误.

将数据加载到GridView中起作用,但是当我想删除一行时,我会收到该错误.

<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 – 我不是真的从数据库中删除行,但它是一个测试:

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

但点击后,我得到一个bigass黄页与下一个错误:

The GridView ‘OrdersGridView’ fired event RowDeleting which wasn’t handled.

解决方法 删除按钮,甚至是具有CommandName的删除的GridView中的常规按钮,将自动尝试启动OnRowDeleting.你可以添加它来使事情变得快乐,但不要做任何事情,所以它不会影响你的删除行为.

您可以将OnRowDeleting添加到GridView中:

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

然后在你的CodeBehind中添加:

void OrdersGridView_RowDeleting (object sender,GridViewDeleteEventArgs e){}