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

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

最佳答案

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

Calling this method also raises the RowDeleted and RowDeleting events.

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

关于c# - gridrow 删除事件导致无限循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18243797/

相关文章:

c# - 使用带有 Windows 身份验证的 Blazor 客户端使用 Web API

c# - 从 EF6 模型自动生成的 TT 文件渲染默认参数说明符不允许错误

asp.net - jQuery黑莓ajax问题

c# - asp.net gridview onrowcommand -->( GridViewCommandEventArgs)

c# - 将 textwriter 分配给 memory writer

c# - 迁移 : No DbContext was found in assembly

html - main.css 中的颜色和字体大小问题。怎么修?

php - yii2 dataprovider 获取所有过滤后的数据

c# - 如何动态地重新排序 gridview 中的列

c# - Xamarin Forms 在绑定(bind)到条目的文本属性时卡住