OnCheckedChanged event handler of asp:checkbox does not fire when checkbox is unchecked

前端 未结 6 1577
我寻月下人不归
我寻月下人不归 2020-12-11 00:24

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 t

相关标签:
6条回答
  • 2020-12-11 00:37

    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.

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

    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.

    0 讨论(0)
  • 2020-12-11 00:39

    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

    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] + ",";
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-11 00:43

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

    chkLinked.CheckedChanged += new EventHandler(chkLinked_CheckedChanged);
    
    0 讨论(0)
  • 2020-12-11 00:43

    Use AutoPostBack="true" like this:

    <asp:CheckBox ID="chkLinked" runat="server" AutoPostBack="true"
        Checked="false" OnCheckedChanged="chkLinked_CheckedChanged" />
    
    0 讨论(0)
  • 2020-12-11 00:47

    Try usingAutoPostBack="true" like this:

    <asp:CheckBox ID="chkLinked" runat="server" Checked="false"
        OnCheckedChanged="chkLinked_CheckedChanged" AutoPostBack="true"/>
    
    0 讨论(0)
  • 2020-12-11 00:48

    Subscribe to the CheckChanged event in your Page_Init.

    0 讨论(0)
提交回复
热议问题