gridrow删除事件导致无限循环

本文关键字:无限循环 事件 删除 gridrow | 更新日期: 2023-09-27 18:27:47

我正试图在我的网格视图中添加一个删除列,但当我按下一行上的删除按钮时,它会无限调用我分配给onRowDelete的函数。

这是我的代码:

  <asp:GridView ID="GridView1" runat="server" CssClass="yep" AutoGenerateColumns="False" OnRowDeleting="dismissClick">
<Columns>
    <asp:TemplateField HeaderText="Create Incident">
        <ItemTemplate>
            <asp:CheckBox ID="Selections" runat="server" ViewStateMode = "Enabled" OnCheckedChanged="CheckBox1_CheckedChanged" />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="case#" HeaderText="Case #" SortExpression="case#" />
    <asp:BoundField DataField="Unit #" HeaderText="Unit #" SortExpression="Unit #" />
    <asp:BoundField DataField="date" HeaderText="date" SortExpression="date" />
    <asp:BoundField DataField="typeOfCall" HeaderText="typeOfCall" SortExpression="typeOfCall" />
    <asp:BoundField DataField="Primary Impression" HeaderText="Primary Impression" SortExpression="Primary Impression" />
    <asp:TemplateField HeaderText="View PCR">
        <ItemTemplate>
            <asp:Button ID="ViewPCR" Text="View PCR" runat="server" OnClick="viewPCRClick" CssClass="btn em" />
        </ItemTemplate>
    </asp:TemplateField>
      <asp:TemplateField HeaderText="Dismiss">
        <ItemTemplate>
            <asp:Button ID="dismiss" Text="Dismiss" runat="server" CommandName="Delete" CssClass="btn em" />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
   </asp:GridView>

以及我的简单删除功能:

   protected void dismissClick(object sender, GridViewDeleteEventArgs e) 
    {
        int index = Convert.ToInt32(e.RowIndex);
        GridView1.DeleteRow(index);
    }

gridrow删除事件导致无限循环

调用GridView1.DeleteRow(index)会导致事件循环,因为调用该方法还会再次引发RowDeletedRowDeleting事件,如MSDN文档中GridView.DeleteRow method:所述

Calling this method also raises the RowDeleted and RowDeleting events.

相反,您希望从绑定源(即DataSetDataTableList<T>或您绑定到的任何对象)中删除它,或者将您的删除保留到数据库或最终保存数据的任何对象,然后重新绑定网格。