views:

810

answers:

5

Even when on the page, the EnableViewState property is disabled, I am still seeing some viewstate existing on the page:

"<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="VkBAB3n5LZYtY+nTzk1vEu1P/6QLf4qzFIKzpFRJe3DMf8UseUA/1RsO409HJX4QhkROSP0umoJvatjK/q+jXA==" />"

My question is why?

+2  A: 

This could be controls that are using ControlState. Any control that has control state will ignore your ViewState settings.

Andrew Hare
So I guess, some of the Built-in ASP.net Controls are using ControlState?
TimLeung
Correct. See: http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/truly-understanding-viewstate.aspx
John Sheehan
I don't think this is accurate. If you make a new web form with no controls on it - there is still some view state.
brendan
@brendan: that's because the `<form runat="server">` tag is a control, and it uses ControlState.
RickNZ
+3  A: 

This article is a little old but to my understanding most of the points are still valid:

  1. You must have a server-side form tag () in your ASPX page if you want to use ViewState. A form field is required so the hidden field that contains the ViewState information can post back to the server. And, it must be a server-side form so the ASP.NET page framework can add the hidden field when the page is executed on the server.
  2. The page itself saves 20 or so bytes of information into ViewState, which it uses to distribute PostBack data and ViewState values to the correct controls upon postback. So, even if ViewState is disabled for the page or application, you may see a few remaining bytes in ViewState.
  3. In cases where the page does not post back, you can eliminate ViewState from a page by omitting the server side tag.

http://msdn.microsoft.com/en-us/library/ms972427.aspx

brendan
+4  A: 

It's the control state.

If you really want to get rid of viewstate and controlstate you can use this code in the code-behind for the page, or in any class that the code-behind derives from

class MyPage : Page {
    private class DummyPageStatePersister : PageStatePersister {
        public DummyPageStatePersister(Page p) : base(p) {}
        public override void Load() {}
        public override void Save() {}
    }
    private DummyPageStatePersister _PageStatePersister;
    protected override PageStatePersister PageStatePersister {
        get {
            if (_PageStatePersister == null)
                _PageStatePersister = new DummyPageStatePersister(this);
            return _PageStatePersister;
        }
    }

    // other stuff comes here
}

Be very careful when doing this, though, since you're violating the contract with the controls. MSDN explicitly states that control state is always available. In practice, however, it has worked for me.

Edit: Since I was downvoted, I like to point out again: Don't do this unless you know exactly what you are doing. In my case, almost the entire application was written in client-side javascript, and on those few occations where postbacks occurred, I always used the Request.Form collection to retrieve the values. Do not use server-side controls for anything but simple rendering if you do this.

erikkallen
-1 this is a very bad idea, you are introducing potential error for very little (if any) benefit
Andrew Hare
Up-votted, not because it's a good idea but because it gives some interesting background.
Gabe
+1 because its a super cool idea. Actually cuz its useful for me when I am doing pure client side functionality and have no need for viewstate and postbacks. And there is definately benefit. I have a control that is generating around 20 pages of pure viewstate for no good use because I am not using it for anything
Sameer Alibhai
A: 

Controlstate can be the causes. Control state can not be disabled. In ASP.NET 2.0 there is a distinction between data necessary to make a control work (controlstate), and other data (viewstate)

And yes some of the controls don't work without controlstate. If you want to know which one is causing it or what the viewstate contains check out a viewstate viewer

Cohen
+1  A: 

This is an absolutely fantastic article on ViewState if you develop in ASP.NET read it!

ASP.NET ViewState Helper is also a nice tool for seeing what's going on in your ViewState

Gavin Miller