2010-03-26 185 views
3

爲什麼Repeater中的按鈕不會觸發Repeater的ItemCommand事件?有沒有辦法強制它這樣做? ViewState已啓用。Repeater中的按鈕不會觸發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); 
    } 
+0

謝謝傑夫lol – 2010-03-26 15:13:24

+0

也許現在很快就可以接受一個答案,如果其中一個解決了問題。我認爲這是重新生成的每一次問題? – 2012-08-30 13:30:17

回答

15

編輯:按您的其他意見,這聽起來像你再結合每個回發的中繼器。當你這樣做時,你會銷燬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方法顯然正在執行,它只是沒有觸及任何情況,因爲這些按鈕沒有定義命令名稱。

+0

事件根本不會觸發。我在事件處理程序的頂部有一個斷點。所有其他頁面事件觸發很好。在兩個按鈕上設置CommandName不起作用。 – 2010-03-26 15:55:45

+1

不知何故,我不認爲按鈕的單擊事件冒泡到中繼器。我重寫了OnBubbleEvent並且按鈕事件沒有被捕獲。 – 2010-03-26 15:57:37

9

當你綁定你的中繼?如果您手動執行此操作,請確保只綁定它,如果該頁面不是回發。

提供一些更多的代碼,請

+0

謝謝 - 我用代碼示例更新了帖子 – 2010-03-26 15:18:24

+0

添加更多代碼。什麼時候調用BindList()?在Page_Load中? – citronas 2010-03-26 15:26:19

+0

我必須在回發時綁定列表,因爲列表綁定的結果標準根據頁面上的下拉列表而不同。用戶從下拉列表中選擇「批准」或「刪除」,單擊搜索(回發),BindList()將數據源綁定到新結果。 – 2010-03-26 15:53:43

1

至於其他2個職位描述

  • 不要重新綁定在回發
  • 確保您設置CommandName屬性上的按鈕

而另一個問題,我有,爲具有的EnableViewState Repeater上的屬性設置爲false,它需要設置爲true。