【问题标题】:GridView.DeleteRow calling 'RowDeleting' on ItemTemplate Click?GridView.DeleteRow 在 ItemTemplate Click 上调用“RowDeleting”?
【发布时间】:2014-06-13 18:08:29
【问题描述】:

大家好,我正在尝试在单击 itemtemplate 按钮时删除一行,但是在我的 OnRowCommand 方法中调用 GridView.DeleteRow 方法时,我收到了错误:

The GridView 'MyGridView' fired event RowDeleting which wasn't handled

据我所知,RowDeleting 只有在您将 CommandName 设置为 Delete 时才会被调用?

这是我的GridViewOnRowCommand 的示例:

网格视图

<asp:GridView ID="MyGridView" runat="server" OnRowCommand="Gv_RowCommand">
        <Columns>
                .
                .
                .
            <asp:TemplateField>
                 <ItemTemplate>
                      <asp:ImageButton ID="ImgBtn" runat="server" CommandName="RemoveRow" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" ImageUrl="img.png" ToolTip="Remove" />
                 </ItemTemplate>
            </asp:TemplateField>
         </Columns>
</asp:GridView>

OnRowCommand 背后的代码

protected void Gv_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "RemoveRow")
            {
                int index = Convert.ToInt32(e.CommandArgument);

                //THROWS EXCEPTION
                Gv_SelectedLineItems.DeleteRow(index);
            }
        }

我一直在 msdn 上关注 this 示例,但到目前为止还没有成功。请帮忙。

【问题讨论】:

  • 您是否考虑过仅使用空处理程序处理事件?
  • 那么创建没有正文的“RowDeleting”处理程序?这是最佳做法吗?

标签: c# asp.net gridview


【解决方案1】:

您在这里遗漏了一件事 - 调用 Gv_SelectedLineItems.DeleteRow(index) 可以期待 RowDeleting 函数。

混淆来自于您已经在按钮回调中,有时删除命令也是如此。

当您在GridViewDeleteRow 时,它要求您拥有此事件 - 希望您能够处理其中可能需要预先删除的任何内容。

您的按钮点击 - "RemoveRow" 与删除无关,它只是一个按钮点击。

编辑:

您需要创建一个 RowDeleting 事件处理程序。在您的代码隐藏中;

protected void Gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

}

您需要在 GridView 的顶部添加事件处理程序:

<asp:GridView ID="MyGridView" runat="server" OnRowCommand="Gv_RowCommand" OnRowDeleting="Gv_RowDeleting">

【讨论】:

  • 感谢@Jono 的回复。您能否提供我可以参考的 sn-p 或资源?我仍然不清楚如何进行。
猜你喜欢
  • 1970-01-01
  • 2015-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-10
相关资源
最近更新 更多