C# 中继器中的按钮不会触发ItemCommand
为什么中继器内的按钮不会触发中继器的ItemCommand事件?有没有办法迫使它这样做?ViewState已启用 在下面的代码中,btnaprove和btnDelete是有问题的按钮:C# 中继器中的按钮不会触发ItemCommand,c#,asp.net,button,repeater,itemcommand,C#,Asp.net,Button,Repeater,Itemcommand,为什么中继器内的按钮不会触发中继器的ItemCommand事件?有没有办法迫使它这样做?ViewState已启用 在下面的代码中,btnaprove和btnDelete是有问题的按钮: <asp:Repeater runat="server" ID="rpt1" onitemdatabound="rpt1_ItemDataBound" onitemcommand="rpt1_ItemCommand" > <ItemTemplate> <tab
<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);
}
挑选
标题:
正文:
///
///处理rpt1控件的ItemCommand事件。
///
///事件的来源。
///包含事件数据的实例。
受保护的无效rpt1_项命令(对象源,RepeaterCommandEventArgs e)
{
var c1=CommentRepository.GetById(Convert.ToUInt64(e.CommandArgument.ToString());
如果(如CommandName==“批准”)
{
c1.批准=正确;
c1.ApprovationUserId=WebAdminContext.RelatedUserId;
}
如果(如CommandName==“拒绝”)
{
c1.批准=错误;
c1.ApprovationSerid=0;
}
如果(如CommandName==“删除”)
{
c1.删除=真;
c1.DeletionUserId=WebAdminContext.RelatedUserId;
}
如果(例如,CommandName==“还原”)
{
c1.删除=错误;
c1.DeletionUserId=0;
}
更新(c1);
重置子序列信息();
BindList();
}
///
///绑定列表。
///
私有void BindList()
{
_条件=lcb1.GenerateCriteriaFromUI();
var sc1=CommentRepository.Filter(
新过滤选项(
EntityListPager1.CurrentSubSequenceInfo,
无效的
CommentRepository.GetCriteriaToFilterByTGID(CurrentEntityGEODEReference.GID)和(_标准)
)
);
//束缚
rpt1.DataSource=sc1.Items;
rpt1.DataBind();
EntityListPager1.BindToUI(sc1.Info);
}
您什么时候绑定中继器?如果是手动操作,请确保仅在页面不是回发时绑定它
请提供更多的代码编辑:根据您的其他评论,听起来您在每次回发时都绑定了转发器。执行此操作时,将销毁
ItemCommand
的事件源—与客户端单击的按钮关联的原始中继器项
用户选择“已批准”或“已批准”
从下拉列表中“删除”,单击
搜索(回发)和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
方法显然正在执行,但它不会影响任何情况,因为这些按钮没有定义命令名。如其他两篇文章所述
- 请勿在回邮时重新绑定
- 确保在按钮上设置CommandName属性
我遇到的另一个问题是,中继器上的EnableViewState属性设置为false,需要将其设置为true。我必须在回发时绑定列表,因为列表绑定到的结果的标准根据页面上的下拉列表而有所不同。用户从下拉列表中选择“已批准”或“已删除”,单击“搜索”(回发),BindList()将数据源绑定到新结果。@标记-您的意思是即使单击“批准”和“删除”按钮,您也在绑定转发器吗?您是否在page指令中将
EnableEventValidation
设置为false
?如果是这样,这就是问题的根源。(如果这是正确的,我可以在我的回答中解释更多。)你不能在每次回发时重新绑定列表。这将使您的RowCommand事件强制丢失。只需重新绑定!在OnRowCommand之后调用的Page.IsPostback和in eventhandlerprocessed@Mark理查曼:我认为一个悬而未决的问题是:中继器什么时候第一次出发?这是用if(!IsPostBack)检查保护的吗?这是我的问题。非常感谢。活动一点也没有启动。我在事件处理程序的顶部有一个断点。所有其他页面事件都可以正常启动。在这两个按钮上设置CommandName没有效果。不知何故,我认为按钮的click事件不会出现在中继器上。我覆盖了BubbleEvent,button事件没有被捕获。如果其中一个解决了问题,也许是时候接受答案了。我想每次都是因为重新绑定的问题?