中继器中的按钮不会触发 ItemCommand

发布于 2024-08-27 02:02:16 字数 4077 浏览 8 评论 0原文

为什么中继器内的按钮不会触发中继器的 ItemCommand 事件?有没有办法强制它这样做?视图状态已启用。

在下面的代码中,btnApprove 和 btnDelete 是有问题的按钮:

<asp:Repeater runat="server" ID="rpt1" onitemdatabound="rpt1_ItemDataBound" onitemcommand="rpt1_ItemCommand" >
    <ItemTemplate>
        <table width="100%" style="margin-bottom:6px;">
            <tr>
                <td>
                    <asp:CheckBox ID="chkSelected" runat="server" Text=" " TextAlign="Right"/> Select
                    <asp:Button ID="btnApprove" runat="server" Width="80px" Text="Approve" />
                    <asp:Button ID="btnDelete" runat="server" Width="80px" Text="Delete" />
                </td>                                                                   
            </tr>
            <tr>
                <td align="right">
                    <asp:Label ID="lblCommentStatus" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
        </table>
        <table width="100%" style="margin-top:6px;">
            <tr>
                <td><asp:Label ID="lblAuthorName" runat="server" Text="Author: " Width="60px"></asp:Label></td>
                <td><asp:TextBox ID="txtAuthorName" runat="server" Width="250px"></asp:TextBox></td>
                <td style="padding-left: 30px;"><asp:Label ID="lblAuthorLocation" runat="server" Text="Location: " Width="70px"></asp:Label></td>
                <td><asp:TextBox ID="txtAuthorLocation" runat="server" Width="250px"></asp:TextBox></td>
            </tr>
        </table>
        Title: <asp:TextBox ID="txtTitle" runat="server" Width="640px" Enabled="False"></asp:TextBox>
        Body: <asp:TextBox ID="txtBody" runat="server" Width="640px" TextMode="MultiLine" Height="60px" Enabled="False"></asp:TextBox>
        <table width="100%" style="margin-top:6px;">
            <tr>
                <td><asp:Label ID="lblVotes" runat="server" Text="Votes: " Width="80px"></asp:Label></td>
                <td><asp:Label ID="lblVotesCount" runat="server" Text="" Width="600px"></asp:Label></td>
            </tr>
        </table>
        <hr style="margin-top:20px; margin-bottom:20px;" />
    </ItemTemplate>
</asp:Repeater>

/// <summary>
  /// Handles the ItemCommand event of the rpt1 control.
  /// </summary>
  /// <param name="source">The source of the event.</param>
  /// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterCommandEventArgs"/> instance containing the event data.</param>
  protected void rpt1_ItemCommand(object source, RepeaterCommandEventArgs e)
  {
    var c1 = CommentRepository.GetById(Convert.ToUInt64(e.CommandArgument.ToString()));

    if (e.CommandName == "approve")
    {
      c1.Approved = true;
      c1.ApprovationUserId = WebAdminContext.RelatedUserId;
    }

    if (e.CommandName == "reject")
    {
      c1.Approved = false;
      c1.ApprovationUserId = 0;
    }

    if (e.CommandName == "delete")
    {
      c1.Deleted = true;
      c1.DeletionUserId = WebAdminContext.RelatedUserId;
    }

    if (e.CommandName == "restore")
    {
      c1.Deleted = false;
      c1.DeletionUserId = 0;
    }

    CommentRepository.Update(c1);

    ResetSubSequenceInfo();
    BindList();
      }

/// <summary>
  /// Binds the list.
  /// </summary>
  private void BindList()
  {
    _Criteria = lcb1.GenerateCriteriaFromUI();

    var sc1 = CommentRepository.Filter(
      new FilteringOptions(
        EntityListPager1.CurrentSubSequenceInfo,
        null,
        CommentRepository.GetCriteriaToFilterByTGID(CurrentEntityGEODEReference.GID).And(_Criteria)
        )
      );

    // BIND
    rpt1.DataSource = sc1.Items;
    rpt1.DataBind();

    EntityListPager1.BindToUI(sc1.Info);
  }

Why would a button inside a Repeater not fire the Repeater's ItemCommand event? Is there a way to force it to do so? ViewState is Enabled.

In the code below, btnApprove and btnDelete are the buttons in question:

<asp:Repeater runat="server" ID="rpt1" onitemdatabound="rpt1_ItemDataBound" onitemcommand="rpt1_ItemCommand" >
    <ItemTemplate>
        <table width="100%" style="margin-bottom:6px;">
            <tr>
                <td>
                    <asp:CheckBox ID="chkSelected" runat="server" Text=" " TextAlign="Right"/> Select
                    <asp:Button ID="btnApprove" runat="server" Width="80px" Text="Approve" />
                    <asp:Button ID="btnDelete" runat="server" Width="80px" Text="Delete" />
                </td>                                                                   
            </tr>
            <tr>
                <td align="right">
                    <asp:Label ID="lblCommentStatus" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
        </table>
        <table width="100%" style="margin-top:6px;">
            <tr>
                <td><asp:Label ID="lblAuthorName" runat="server" Text="Author: " Width="60px"></asp:Label></td>
                <td><asp:TextBox ID="txtAuthorName" runat="server" Width="250px"></asp:TextBox></td>
                <td style="padding-left: 30px;"><asp:Label ID="lblAuthorLocation" runat="server" Text="Location: " Width="70px"></asp:Label></td>
                <td><asp:TextBox ID="txtAuthorLocation" runat="server" Width="250px"></asp:TextBox></td>
            </tr>
        </table>
        Title: <asp:TextBox ID="txtTitle" runat="server" Width="640px" Enabled="False"></asp:TextBox>
        Body: <asp:TextBox ID="txtBody" runat="server" Width="640px" TextMode="MultiLine" Height="60px" Enabled="False"></asp:TextBox>
        <table width="100%" style="margin-top:6px;">
            <tr>
                <td><asp:Label ID="lblVotes" runat="server" Text="Votes: " Width="80px"></asp:Label></td>
                <td><asp:Label ID="lblVotesCount" runat="server" Text="" Width="600px"></asp:Label></td>
            </tr>
        </table>
        <hr style="margin-top:20px; margin-bottom:20px;" />
    </ItemTemplate>
</asp:Repeater>

/// <summary>
  /// Handles the ItemCommand event of the rpt1 control.
  /// </summary>
  /// <param name="source">The source of the event.</param>
  /// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterCommandEventArgs"/> instance containing the event data.</param>
  protected void rpt1_ItemCommand(object source, RepeaterCommandEventArgs e)
  {
    var c1 = CommentRepository.GetById(Convert.ToUInt64(e.CommandArgument.ToString()));

    if (e.CommandName == "approve")
    {
      c1.Approved = true;
      c1.ApprovationUserId = WebAdminContext.RelatedUserId;
    }

    if (e.CommandName == "reject")
    {
      c1.Approved = false;
      c1.ApprovationUserId = 0;
    }

    if (e.CommandName == "delete")
    {
      c1.Deleted = true;
      c1.DeletionUserId = WebAdminContext.RelatedUserId;
    }

    if (e.CommandName == "restore")
    {
      c1.Deleted = false;
      c1.DeletionUserId = 0;
    }

    CommentRepository.Update(c1);

    ResetSubSequenceInfo();
    BindList();
      }

/// <summary>
  /// Binds the list.
  /// </summary>
  private void BindList()
  {
    _Criteria = lcb1.GenerateCriteriaFromUI();

    var sc1 = CommentRepository.Filter(
      new FilteringOptions(
        EntityListPager1.CurrentSubSequenceInfo,
        null,
        CommentRepository.GetCriteriaToFilterByTGID(CurrentEntityGEODEReference.GID).And(_Criteria)
        )
      );

    // BIND
    rpt1.DataSource = sc1.Items;
    rpt1.DataBind();

    EntityListPager1.BindToUI(sc1.Info);
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼波传意 2024-09-03 02:02:16

编辑:根据您的其他评论,听起来您在每次回发时都重新绑定了中继器。当您这样做时,您会销毁 ItemCommand 的事件源 - 与客户端单击的按钮关联的原始 Repeater 项目。

用户选择“已批准”或
从下拉列表中“删除”,点击
搜索(回发)和 BindList()
将数据源绑定到新的
结果。

您可以在下拉列表的处理程序中重新绑定转发器,只需确保您没有在“批准”或“删除”按钮启动的执行路径期间执行此操作即可。


可能还有另一个问题,但您肯定需要为按钮指定命令名称才能使该代码正常工作:

<asp:Button ID="btnApprove" runat="server" Width="80px" Text="Approve" CommandName="approve"/>
<asp:Button ID="btnDelete" runat="server" Width="80px" Text="Delete" CommandName="delete"/>

我无法重现该问题:您确定 ItemCommand 处理程序甚至没有触发吗?使用代码的稍微修改版本,当我单击“批准”或“删除”时,我的 rpt1_ItemCommand 方法显然正在执行,它只是没有遇到任何情况,因为这些按钮不已定义命令名称。

Edit: per your other comments, it sounds like you're re-binding the repeater on every postback. When you do that, you destroy the ItemCommand's event source - the original Repeater item associated with the button the client clicked.

The user selects "approved" or
"deleted" from a dropdown, clicks
search (a postback) and BindList()
binds the datasource the to new
results.

You can re-bind the repeater in your drop-down's handler, just make sure you aren't doing it during the execution path initiated by your 'Approve' or 'Delete' buttons.


There may be another issue, but you definitely need to specify command names for your buttons for that code to work:

<asp:Button ID="btnApprove" runat="server" Width="80px" Text="Approve" CommandName="approve"/>
<asp:Button ID="btnDelete" runat="server" Width="80px" Text="Delete" CommandName="delete"/>

I can't reproduce the problem: are you sure the ItemCommand handler isn't even firing? Using a slightly modified version of your code, my rpt1_ItemCommand method is clearly executing when I click 'Approve' or 'Delete', it just isn't hitting any of the cases, because those buttons don't have command names defined.

只是在用心讲痛 2024-09-03 02:02:16

你什么时候绑定中继器?如果您手动执行此操作,请确保仅在页面不是回发时才绑定它。

请提供更多代码

When do you bind your repeater? If you do it manually, be sure that you only bind it, if the page is not a postback.

Provide some more code please

早茶月光 2024-09-03 02:02:16

正如其他 2 篇文章所描述的,

  • 不要在回发上重新绑定,
  • 请确保在按钮上设置 CommandName 属性

我遇到的另一个问题是,将 Repeater 上的 EnableViewState 属性设置为 false,需要将其设置为 true。< /强>

As the other 2 posts describe

  • Do not rebind on PostBack
  • Make sure you set the CommandName property on the Button

And another problem i had, was having EnableViewState property on the Repeater set to false, it needs to be set to true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文