取消选中复选框时,asp:checkbox 的 OnCheckedChanged 事件处理程序不会触发
我有一个中继器,在中继器的每个 ItemTemplate 中都是一个带有 OnCheckedChanged 事件处理程序集的 asp:checkbox。这些复选框的 AutoPostBack 属性设置为 true。当选中任何复选框时,将触发事件处理程序。当取消选中任何一个时,事件处理程序不会触发。
知道为什么该事件没有触发,以及我如何让它触发吗?谢谢。
简化的转发器代码:
<asp:Repeater ID="rptLinkedItems" runat="server">
<ItemTemplate>
<asp:CheckBox ID="chkLinked" runat="server"
Checked="false" OnCheckedChanged="chkLinked_CheckedChanged" />
</ItemTemplate>
</asp:Repeater>
集合绑定到转发器,如下所示:
protected override void OnPreRenderComplete(EventArgs e)
{
if (!Page.IsPostBack)
{
m_linkedItems = GetLinkedItems();
rptLinkedItems.DataSource = GetLinkableItems();
rptLinkedItems.ItemDataBound += new RepeaterItemEventHandler
(rptLinkedItems_ItemDataBound);
rptLinkedItems.DataBind();
}
base.OnPreRenderComplete(e);
}
OnItemDataBound 事件处理程序如下:
private void rptLinkedItems_ItemDataBound(Object sender, RepeaterItemEventArgs args)
{
if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
{
CategoryItem item = args.Item.DataItem as CategoryItem;
Literal litItemName = args.Item.FindControl("litItemName") as Literal;
CheckBox chkLinked = args.Item.FindControl("chkLinked") as CheckBox;
litItemName.Text = item.Text;
chkLinked.Checked = IsItemLinked(item);
chkLinked.AutoPostBack = true;
chkLinked.InputAttributes.Add("Value", item.Id.ToString());
}
}
OnCheckedChanged 事件处理程序如下:
protected void chkLinked_CheckedChanged(Object sender, EventArgs args)
{
CheckBox linkedItem = sender as CheckBox;
Boolean itemState = linkedItem.Checked;
Int32 itemId = Int32.Parse(linkedItem.InputAttributes["Value"].ToString());
DataAccessLayer.UpdateLinkedItem(m_linkingItem, Utilities.GetCategoryItemFromId(itemId), itemState);
}
PS 如果有人也可以告诉我为什么 markdown 对我来说不能正常工作......
I have a repeater, in each ItemTemplate of the repeater is an asp:checkbox with an OnCheckedChanged event handler set. The checkboxes have the AutoPostBack property set to true. When any of the checkboxes is checked, the event handler fires. When any is unchecked, the event handler does not fire.
Any idea why the event does not fire, and how I mgiht make it fire? Thanks.
Simplified repeater code:
<asp:Repeater ID="rptLinkedItems" runat="server">
<ItemTemplate>
<asp:CheckBox ID="chkLinked" runat="server"
Checked="false" OnCheckedChanged="chkLinked_CheckedChanged" />
</ItemTemplate>
</asp:Repeater>
The collection is bound to the repeater as follows:
protected override void OnPreRenderComplete(EventArgs e)
{
if (!Page.IsPostBack)
{
m_linkedItems = GetLinkedItems();
rptLinkedItems.DataSource = GetLinkableItems();
rptLinkedItems.ItemDataBound += new RepeaterItemEventHandler
(rptLinkedItems_ItemDataBound);
rptLinkedItems.DataBind();
}
base.OnPreRenderComplete(e);
}
The OnItemDataBound event handler is as follows:
private void rptLinkedItems_ItemDataBound(Object sender, RepeaterItemEventArgs args)
{
if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
{
CategoryItem item = args.Item.DataItem as CategoryItem;
Literal litItemName = args.Item.FindControl("litItemName") as Literal;
CheckBox chkLinked = args.Item.FindControl("chkLinked") as CheckBox;
litItemName.Text = item.Text;
chkLinked.Checked = IsItemLinked(item);
chkLinked.AutoPostBack = true;
chkLinked.InputAttributes.Add("Value", item.Id.ToString());
}
}
The OnCheckedChanged event handler is as follows:
protected void chkLinked_CheckedChanged(Object sender, EventArgs args)
{
CheckBox linkedItem = sender as CheckBox;
Boolean itemState = linkedItem.Checked;
Int32 itemId = Int32.Parse(linkedItem.InputAttributes["Value"].ToString());
DataAccessLayer.UpdateLinkedItem(m_linkingItem, Utilities.GetCategoryItemFromId(itemId), itemState);
}
P.S. If someone can also tell me why markdown doesn't work correctly for me...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试使用
AutoPostBack="true"
,如下所示:Try using
AutoPostBack="true"
like this:这是因为当 ASP.NET 执行 ASP.NET 页面生命周期的
Control events
部分时,控件层次结构(特别是复选框)不存在,就像您在稍后的 PreRender 阶段。请参阅 ASP.NET 页面生命周期概述,了解更详细的概述事件顺序。对于 @bleeeah 的建议,我会谨慎行事,因为您正在为
rptLinkedItems_ItemDataBound
内的CheckBox.Checked
分配一个值,这也会导致该事件要执行的处理程序:相反,将:
移至
Page.Load
事件处理程序中。This is because the control hierarchy (and the check boxes in particular) don't exist when ASP.NET executes the
Control events
portion of the ASP.NET page life cycle, as you had created them in the laterPreRender
stages. Please see ASP.NET Page Life Cycle Overview for more detailed overview of the event sequence.I would err on the side of caution for @bleeeah's advice, for you're assigning a value to
CheckBox.Checked
insiderptLinkedItems_ItemDataBound
, which would also cause the event handler to execute:Instead, move:
Into the
Page.Load
event handler.尝试重新订阅 OnItemDataBound 事件中的 CheckChanged 事件,
Try re-subscribing to the CheckChanged event in your OnItemDataBound event ,
像这样使用
AutoPostBack="true"
:Use
AutoPostBack="true"
like this:订阅 Page_Init 中的 CheckChanged 事件。
Subscribe to the CheckChanged event in your Page_Init.
您必须在中继器项目命令之外为清单定义事件处理程序,然后在中继器项目命令内,遍历清单项目并获取已检查的项目。
在 .aspx 页面中,您可以使用 Ajax 和 updatepanel 来触发事件处理程序,但请记住,您必须在中继器之外定义 scriptmanage。
// checklisk checkchange 事件处理程序
和项目重复器命令 item:
// 迭代清单项并检测已检查
You have to define eventhandler for checklist out of repeater item command, then inside the repeater item command, go through checklist items and get checked items.
In the .aspx page you can use Ajax and updatepanel to fire eventhandler, but keep in mind you have to define scriptmanage outside of repeater.
// checklisk checkedchange eventhandler
and item repeater command item:
// iterate checklist items and detect checked