3

I'm trying to add a delete column to my gridview, however when I press the delete button on a row, it calls the function I assigned to onRowDeleting infinitely or so it seems.

Here's my code:

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

and my simple deletion function:

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

1 回答 1

1

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

Calling this method also raises the RowDeleted and RowDeleting events.

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

于 2013-08-14T23:55:55.433 回答