How to completely disable ViewState and ControlState

Here is a code snippet that will COMPLETELY disable ViewState and ControlState.

Please note, if you want to disable viewstate, you can set “EnableViewState” to false for the page, however you will still see “VIEWSTATE” in the page. The reason for that is because the hidden ViewState HTML field also contains “Control State”, which is used by server controls (either built in controls or custom 3rd party controls) and is not disable-able (for core functionality that their control depends on).

However you can still disable this, with disastrous consequences on postback.

USE THIS only if you have NO POSTBACKS whatsoever and you are living in a happy client side world of javascript and web service calls.

Put this following code inside your ASP.NET 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;
    }
}

Please note this will has disastrous consequences if you attempt to do a postback because you have essentially killed the control state.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>