Search code examples
c#asp.neteventscheckboxupdatepanel

CheckBox does not fire CheckedChanged event when unchecking the checkbox Dynamically in ASP.Net


Here I am having a checkbox in this update panel.

<asp:UpdatePanel runat="server" ID="LVCheckBoxes">
    <ContentTemplate>
            <ul>
                <li>
                    <asp:CheckBox ID="New" Text="New" AutoPostBack="true" runat="server" OnCheckedChanged="checkBoxCheck" /></li>
            </ul>
    </ContentTemplate>
</<asp:UpdatePanel >

All I am Trying to do is uncheck it on click of linkbutton. Which is working fine.

<asp:LinkButton id="LbUnCheck" runat="server" OnClick="LbUnCheck" ToolTip="New">UnCheck</asp:LinkButton>

But Along with it I also want to fire the check changed Event Which is not firing

protected void LbUnCheck(object sender, EventArgs e)
{
        LinkButton lb = (LinkButton)sender;
        CheckBox cb = (CheckBox)LVCheckBoxes.FindControl(lb.ToolTip);
        cb.CheckedChanged += checkBoxCheck;
        cb.Checked = false;

}


protected void checkBoxCheck(object sender, EventArgs e)
{
   //Do something

}

I even Tried this using JQuery but it is not working quite as expected.

Help Appreciated..!!


Solution

  • Try calling the CheckBox check event method (checkBoxCheck) from the Link Button click event method (LbUnCheck). Something like this:

    protected void LbUnCheck(object sender, EventArgs e)
    {
            LinkButton lb = (LinkButton)sender;
            CheckBox cb = (CheckBox)LVCheckBoxes.FindControl(lb.ToolTip);
    
            // cb.CheckedChanged += checkBoxCheck; // REMOVE THIS LINE
    
            cb.Checked = false;
            checkBoxCheck(null, null); // NOTICE THIS IS AFTER SETTING cb.Checked = false;
    }
    
    
    protected void checkBoxCheck(object sender, EventArgs e)
    {
       //Do something
    }