簡體   English   中英

Repeater中的按鈕不會觸發ItemCommand

[英]Button in a Repeater does not fire ItemCommand

為什么Repeater中的按鈕不會觸發Repeater的ItemCommand事件? 有沒有辦法強迫它這樣做? ViewState已啟用。

在下面的代碼中,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);
  }

編輯 :根據您的其他評論,聽起來您在每次回發時重新綁定轉發器。 當您這樣做時,您將銷毀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方法顯然正在執行,它只是沒有遇到任何情況,因為這些按鈕沒有定義命令名。

你什么時候綁定你的中繼器? 如果您手動執行此操作,請確保僅綁定它,如果頁面不是回發。

請提供更多代碼

正如其他2個帖子所述

  • 不要在PostBack上重新綁定
  • 確保在Button上設置CommandName屬性

我遇到的另一個問題是,將Repeater上的EnableViewState屬性設置為false,需要將其設置為true。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM