2010-03-26 129 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。