ASP.NET CheckBox does not fire CheckedChanged even

2020-02-02 12:33发布

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

<asp:CheckBox runat="server" ID="chkTest" AutoPostBack="true" OnCheckedChanged="chkTest_CheckedChanged" />

In my code behind I have the following method:

protected void chkTest_CheckedChanged(object sender, EventArgs e)
{
}

When I load the page in the browser and click the CheckBox it becomes checked, the page posts back, and I can see chkTest_CheckedChanged being called.

When I then click the CheckBox again it becomes unchecked, the page posts back, however chkTest_CheckedChanged is not called.

The process is repeatable, so once the CheckBox is unchecked, checking it will fire the event.

I have View State disabled in the Web.Config, enabling View State causes this issue to disappear. What can I do to have reliable event firing while the View State remains disabled?

Update: If I set Checked="true" on the server tag the situation becomes reversed with the event firing when un-checking the CheckBox, but not the other way around.

Update 2: I've overridden OnLoadComplete in my page and from within there I can confirm that Request.Form["__EVENTTARGET"] is set correctly to the ID of my CheckBox.

9条回答
啃猪蹄的小仙女
2楼-- · 2020-02-02 12:57

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"
查看更多
走好不送
3楼-- · 2020-02-02 13:01

It's an old post but I had to share my simple solution in order to help others who searched for this problem.

The solution is simple: Turn on AutoPostBack.

        <asp:CheckBox id="checkbox1" runat="server"
                AutoPostBack="True" //<<<<------
                Text="checkbox"
                OnCheckedChanged="knowJobCBOX_CheckedChanged"/>
查看更多
男人必须洒脱
4楼-- · 2020-02-02 13:02

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.

查看更多
登录 后发表回答