gridrow删除事件导致无限循环?

时间:2013-08-14 23:05:09

标签: c# asp.net gridview

我正在尝试向我的gridview添加一个删除列,但是当我按行上的删除按钮时,它会无限地调用我分配给onRowDeleting的函数。

这是我的代码:

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

1 个答案:

答案 0 :(得分:1)

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

Calling this method also raises the RowDeleted and RowDeleting events.

相反,您希望将其从绑定源(即DataSetDataTableList<T>或您绑定的任何内容中删除)或将删除持久保存到数据库或其他任何内容最终保存你的数据,然后重新绑定网格。