当取消选中复选框时,asp:复选框的OnCheckedChanged事件处理程序不会触发

时间:2020-12-18 00:01:37

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.

我有一个转发器,在转发器的每个ItemTemplate中都是一个带有OnCheckedChanged事件处理程序集的asp:复选框。复选框将AutoPostBack属性设置为true。选中任何复选框时,将触发事件处理程序。如果未选中任何内容,则不会触发事件处理程序。

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:

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

The OnCheckedChanged event handler is as follows:

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

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

附:如果有人也可以告诉我为什么降价对我来说不正常...

6 个解决方案

#1


16  

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.

这是因为当ASP.NET执行ASP.NET页面生命周期的Control事件部分时,控件层次结构(特别是复选框)不存在,就像您在后面的PreRender阶段中创建它们一样。有关事件序列的更详细概述,请参阅ASP.NET页面生命周期概述。

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:

我会谨慎对待@ bleeeah的建议,因为你在rptLinkedItems_ItemDataBound中为CheckBox.Checked赋值,这也会导致事件处理程序执行:


chkLinked.Checked = IsItemLinked(item);

Instead, move:

相反,移动:


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

Into the Page.Load event handler.

进入Page.Load事件处理程序。

#2


35  

Try usingAutoPostBack="true" like this:

尝试使用AutoPostBack =“true”,如下所示:

<asp:CheckBox ID="chkLinked" runat="server" Checked="false"
    OnCheckedChanged="chkLinked_CheckedChanged" AutoPostBack="true"/>

#3


5  

Try re-subscribing to the CheckChanged event in your OnItemDataBound event ,

尝试重新订阅OnItemDataBound事件中的CheckChanged事件,

chkLinked.CheckedChanged += new EventHandler(chkLinked_CheckedChanged);

#4


3  

Use AutoPostBack="true" like this:

像这样使用AutoPostBack =“true”:

<asp:CheckBox ID="chkLinked" runat="server" AutoPostBack="true"
    Checked="false" OnCheckedChanged="chkLinked_CheckedChanged" />

#5


2  

Subscribe to the CheckChanged event in your Page_Init.

订阅Page_Init中的CheckChanged事件。

#6


0  

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.

您必须在repeater item命令中为checklist定义eventhandler,然后在repeater item命令中,检查清单项并获取检查项。

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.

在.aspx页面中,您可以使用Ajax和updatepanel来触发事件处理程序,但请记住,您必须在转发器之外定义脚本管理。

// checklisk checkedchange eventhandler

// checklisk checkedchange eventhandler

protected void chkLinked_CheckedChanged(Object sender, EventArgs args)
        {
        }

and item repeater command item: // iterate checklist items and detect checked

和项目转发器命令项://迭代核对表项并检测已检查

    protected void Repeater1_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        CheckBoxList cbl = (CheckBoxList)e.Item.FindControl("CheckBoxList1");
        cbl.SelectedIndexChanged += new EventHandler(chkLinked_CheckedChanged);

        string name = "";
        for (int i = 0; i < cbl.Items.Count; i++)
        {
            if (cbl.Items[i].Selected)
            {
                name += cbl.Items[i].Text.Split(',')[0] + ",";
            }
        }
    }

#1


16  

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.

这是因为当ASP.NET执行ASP.NET页面生命周期的Control事件部分时,控件层次结构(特别是复选框)不存在,就像您在后面的PreRender阶段中创建它们一样。有关事件序列的更详细概述,请参阅ASP.NET页面生命周期概述。

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:

我会谨慎对待@ bleeeah的建议,因为你在rptLinkedItems_ItemDataBound中为CheckBox.Checked赋值,这也会导致事件处理程序执行:


chkLinked.Checked = IsItemLinked(item);

Instead, move:

相反,移动:


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

Into the Page.Load event handler.

进入Page.Load事件处理程序。

#2


35  

Try usingAutoPostBack="true" like this:

尝试使用AutoPostBack =“true”,如下所示:

<asp:CheckBox ID="chkLinked" runat="server" Checked="false"
    OnCheckedChanged="chkLinked_CheckedChanged" AutoPostBack="true"/>

#3


5  

Try re-subscribing to the CheckChanged event in your OnItemDataBound event ,

尝试重新订阅OnItemDataBound事件中的CheckChanged事件,

chkLinked.CheckedChanged += new EventHandler(chkLinked_CheckedChanged);

#4


3  

Use AutoPostBack="true" like this:

像这样使用AutoPostBack =“true”:

<asp:CheckBox ID="chkLinked" runat="server" AutoPostBack="true"
    Checked="false" OnCheckedChanged="chkLinked_CheckedChanged" />

#5


2  

Subscribe to the CheckChanged event in your Page_Init.

订阅Page_Init中的CheckChanged事件。

#6


0  

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.

您必须在repeater item命令中为checklist定义eventhandler,然后在repeater item命令中,检查清单项并获取检查项。

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.

在.aspx页面中,您可以使用Ajax和updatepanel来触发事件处理程序,但请记住,您必须在转发器之外定义脚本管理。

// checklisk checkedchange eventhandler

// checklisk checkedchange eventhandler

protected void chkLinked_CheckedChanged(Object sender, EventArgs args)
        {
        }

and item repeater command item: // iterate checklist items and detect checked

和项目转发器命令项://迭代核对表项并检测已检查

    protected void Repeater1_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        CheckBoxList cbl = (CheckBoxList)e.Item.FindControl("CheckBoxList1");
        cbl.SelectedIndexChanged += new EventHandler(chkLinked_CheckedChanged);

        string name = "";
        for (int i = 0; i < cbl.Items.Count; i++)
        {
            if (cbl.Items[i].Selected)
            {
                name += cbl.Items[i].Text.Split(',')[0] + ",";
            }
        }
    }