ASP.NET CheckBox does not fire CheckedChanged event when unchecking

前端 未结 9 693
隐瞒了意图╮
隐瞒了意图╮ 2020-12-08 10:04

I have a CheckBox on an ASP.NET Content Form like so:



        
相关标签:
9条回答
  • 2020-12-08 10:39

    I had the same problem. I have spent a lot of time on it and finally have solved it.

    In my case the Checkbox was disabled by default:

    <asp:CheckBox ID="chkActive" runat="server" Enabled="false"/>
    

    It turns ViewState isn't loaded for disabled or invisible controls. So remove Enabled="false" or Visible="false" and it will work as expeceted. And of course ViewState shouldn't be disabled.

    0 讨论(0)
  • 2020-12-08 10:44

    Additionally: Check for any errors in the JavaScript console.

    I experienced the same exact issue described by OP except that it only happened in Safari (checkbox worked fine in Chrome and Firefox). Upon inspecting the JavaScript console, I found an error that was being thrown by a malformed jQuery selector.

    In my case, I had $('a[id*=lbView') which was missing a closing ]. This threw an error in Safari but, surprisingly, not in Chrome nor in Firefox.

    0 讨论(0)
  • 2020-12-08 10:46

    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".

    0 讨论(0)
  • 2020-12-08 10:53

    I'm not sure but I guess that my solution is working only for .NET Framework 4.0:

    Use ViewStateMode = "Disabled" to disable view state insted of EnableViewState="false". This will caution the same behavior except that you can save a local view state.

    So, on your checkbox, set the attribute ViewStateMode = "Enabled" and the problem is solved, without implementing a custom checkbox.

    0 讨论(0)
  • 2020-12-08 10:55

    The super easy answer is to set the ViewState on for that one control.

    Just add the EnableViewState="true" to the AutoPostBack="true" property in the checkbox tag.

    0 讨论(0)
  • 2020-12-08 10:57

    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.

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