ViewState, CheckBox, ControlState...errr...

J. Michael Palermo writes about the CheckBox in ASP.NET maintaining its state regardless of the EnableViewState attribute to False in ASP.NET 2.0.

Hang on - isn't that the same in ASP.NET 1.0 and 1.1? Oh yes.

All web controls that implement the IPostBackDataHandler maintain their basic 'value-state' (for lack of a better description) using the HTTP POST form collection. Irrespective of setting the EnableViewState property for the web control to False.

Nothing has changed in ASP.NET 2.0 as far as that is concerned. The CheckBox control along with TextBox and other controls that implement the IPostBackDataHandler interface, still maintain their basic value-state using their respective HTTP POST values. ControlState has nothing to do with this.

ControlState is a way to allow more fine-grained control over individual portions or behavioural elements of a web control, which under the bonnet actually still uses the ViewState statebag. Fritz Onion's article outlines this nicely and in more detail.

12 Comments

  • Stop confusing people. Palermo's point is still valid.

    Yes controls that implement IPostBackDataHandler can get the current value from the HTTP POST operation.

    However what Palermo was referring to was that in order for controls to implement the OnChanged event (or whatever it is for the CheckBox), you need to know both the previous and new value. You can only get the new/current value from the postback.

    In .NET 1 and 1.1 these values were stored in ViewState, and if you disabled ViewState such functionality would 'break.

    In .NET 2 they have introduced ControlState, and this previous value is stored in ControlState for the CheckBox.

    ControlState and ViewState are stored together within the hidden _ViewState field. However this change means that you can turn off ViewState and still have methods like OnChanged still work :-)

    I hope that clears things up.

    David

  • You obviously read one heck of a lot more into that post. Where do you read that Palermo is talking about the behavioural aspect (ie. events) of the control?

    The behavioural elements, OnCheckChanged event in this case, does not get stored in Control State. You would have to implement it yourself.

    The event only fires when changing from unchecked (which it always is as the getter for Checked is null) to checked. Changing back from checked to unchecked and performing a postback doesn't make the event fire, since the default Checked value (not checked) is compared to the value of the checkbox in the POST collection which checks against string.IsNullOrEmpty(checkboxpostvalue) and as such the return value for the LoadPostData method of the checkbox returns false, ie. doesn't raise the OnCheckChanged event.

    This is the same in ASP.NET 1.1 and 2.0.

    Unless you use Control state and persist the event states yourself. But you need to implement it.

    Now stop confusing people.

  • Palermo's point didn't seem to really make a point, he just said that it use one thing and not annother.

    Between Wim's post and David's comment, I think we have a clear idea why.


    I sure hope that not all of the CheckBox's state is being put into ControlState because then the ability to turn off ViewState becomes irrelevant.

  • Cool comments. If you download Fritz Onion's ViewState Decoder, you will see that the state of the CheckBox is stored in ControlState - not ViewState. And you CAN NOT turn off the state of the CheckBox control using EnableViewState="false". That is all my original blog was about.

    - J. Michael Palermo IV

  • Michael, I made a little test in ASP.NET 2.0 which seemed to suggest otherwise.

    I hooked into the OnCheckChanged event of the checkbox and then displayed "Changed!" in the Text of a label control, which I reset on Page_Load.

    When EnableViewState is set to false, it does only fire when you change from unchecked to checked, not the other way around for the reasons I tried to point out in on of the comments above.

  • Yes, this IS an interesting fact in ASP.NET 2.0.

    Good that they did segregated ControlState and ViewState now.

    I was just wondering why my checkbox won't turn off even if i have set enableviewstate=false. Before reading this article i did do a Base64-Decode over __VIEWSTATE field and it revealed all of my checkboxes.

    When i read this article, it made perfect sense. Good one.

    Thanks much!!

    NeoDarwinian
    --
    A little bit of gibberish tehre here and.

  • When the checkbox value is changed from Checked to Unchecked and Posted, the new value of the checkbox (Checked=Off) is not sent as part of Form/QueryString variable. This is sent only when it is checked.

    This may be the reason why checkbox server control doesn't detect and fire the event.

  • I had the same problem, the checkbox value can,t be maintain then i will disable its viewstate Then it will work.

  • Interesting and informative. But will you write about this one more?

  • Valuable thoughts and advices. I read your topic with great interest.

  • one question:

    if the checkbox control is Enabled = false ..., why the control work fine?

    regards.

    .SayaMan.

  • folks i am really confused wether:-
    1. TextBox stors its value in ControlState or not.
    2. TextBox only use ViewState to stores its value and sends the value in response at the time of postback and the reterieve it from request.

    Senario :-

    TextBox's textChange event is getting fired every time if set EnableViewState="false" for this TextBox. and then input any arbitrary value into textbox incide the browser and clicking the button for postback.

    The textbox ChangeEvent is getting fired for all consecutive postback even without changing the value inside the textbox.

    please explain in details, pls...

    Inline Cod:-












    .CS File Code:-

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void txtCheckControlState_TextChanged(object sender, EventArgs e)
    {
    lblShowValues.Text = DateTime.Now.ToString();
    }

    ------------------------------------------------------
    Pls explaing in details how things are working.



Comments have been disabled for this content.