ASP.NET CheckBox does not fire CheckedChanged event when unchecking ASP.NET CheckBox does not fire CheckedChanged event when unchecking asp.net asp.net

ASP.NET CheckBox does not fire CheckedChanged event when unchecking


To fire CheckedChanged event set the following properties for CheckBox, AutoPostBack property should be true and should have a default value either checked false or true.

AutoPostBack="true" Checked="false"


Implementing a custom CheckBox that stores the Checked property in ControlState rather than ViewState will probably solve that problem, even if the check box has AutoPostBack=false

Unlike ViewState, ControlState cannot be disabled and can be used to store data that is essential to the control's behavior.

I don't have a visual studio environnement right now to test, but that should looks like this:

public class MyCheckBox : CheckBox{    private bool _checked;    public override bool Checked { get { return _checked; } set { _checked = value; } }    protected override void OnInit(EventArgs e)    {        base.OnInit(e);        //You must tell the page that you use ControlState.        Page.RegisterRequiresControlState(this);    }    protected override object SaveControlState()    {        //You save the base's control state, and add your property.        object obj = base.SaveControlState();        return new Pair (obj, _checked);    }    protected override void LoadControlState(object state)    {        if (state != null)        {            //Take the property back.            Pair p = state as Pair;            if (p != null)            {                base.LoadControlState(p.First);                _checked = (bool)p.Second;            }            else            {                base.LoadControlState(state);            }        }    }}

more info here.


It doesn't fire because with viewstate disabled the server code does not know that the checkbox was previously checked, therefore it doesn't know the state changed. As far as asp.net knows the checkbox control was unchecked before the postback and is still unchecked. This also explains the reverse behavior you see when setting Checked="true".