gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:52:40 25 4
gpt4 key购买 nike

我正在尝试向我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com