How to completely disable ViewState and ControlState

Posted by Sf_Hussain786 under C# category on | Points: 40 | Views : 1952
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
view sourceprint?
01.private class DummyPageStatePersister <img src="" alt=":P" class="wp-smiley"> ageStatePersister
03.public DummyPageStatePersister(Page p) : base(p) { }
04.public override void Load() { }
05.public override void Save() { }
08.private DummyPageStatePersister _PageStatePersister;
09.protected override PageStatePersister PageStatePersister
13.if (_PageStatePersister == null)
14._PageStatePersister = new DummyPageStatePersister(this);
15.return _PageStatePersister;

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

Comments or Responses

Login to post response