为什么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项.
The user selects “approved” or
“deleted” from a dropdown,clicks
search (a postback) and BindList()
binds the datasource the to new
results.
您可以在下拉列表处理程序中重新绑定转发器,只需确保在“批准”或“删除”按钮启动的执行路径中不执行此操作.
可能还有另一个问题,但您肯定需要为按钮指定命令名称才能使该代码起作用:
<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方法显然正在执行,它只是没有遇到任何情况,因为这些按钮没有定义命令名.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。