本文介绍了ASP的OnCheckedChanged事件处理程序:当复选框被选中复选框不火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个中继器,在中继器的每一个ItemTemplate中是一个asp:复选框与OnCheckedChanged事件处理程序集。该复选框有AutoPostBack属性设置为true。当任何复选框被选中,在事件处理程序的火灾。当任何未被选中,事件处理程序不火。

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.

任何想法,为什么事件不火,我怎么mgiht让它火?谢谢你。

Any idea why the event does not fire, and how I mgiht make it fire? Thanks.

简体中继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);
        }

该OnItemDataBound事件处理程序如下:

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());
            }
        }

该OnCheckedChanged事件处理程序如下:

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。如果有人可以告诉我为什么降价不为我正常工作...

P.S. If someone can also tell me why markdown doesn't work correctly for me...

推荐答案

这是因为,当ASP.NET执行控件事件不存在控制体系(特别是复选框) ASP.NET页面生命周期的一部分,因为你已经在后来的 preRender 阶段创造了他们。请参阅事件序列的更详细的介绍。

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 later PreRender stages. Please see ASP.NET Page Life Cycle Overview for more detailed overview of the event sequence.

我会宁可谨慎为@ bleeeah的意见身边,为你在分配值 CheckBox.Checked rptLinkedItems_ItemDataBound ,这也将导致执行事件处理程序:

I would err on the side of caution for @bleeeah's advice, for you're assigning a value to CheckBox.Checked inside rptLinkedItems_ItemDataBound, which would also cause the event handler to execute:


chkLinked.Checked = IsItemLinked(item);

相反,移动:


if (!Page.IsPostBack)
   {
      m_linkedItems = GetLinkedItems();
      rptLinkedItems.DataSource = GetLinkableItems();
      rptLinkedItems.ItemDataBound += new RepeaterItemEventHandler
          (rptLinkedItems_ItemDataBound);
      rptLinkedItems.DataBind();
   }

Page.Load 事件处理程序。

这篇关于ASP的OnCheckedChanged事件处理程序:当复选框被选中复选框不火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:42