How to completely disable ViewState and ControlState

Sf_Hussain786
Posted by Sf_Hussain786 under C# category on | Points: 40 | Views : 2136
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="http://www.sharpdeveloper.net/content/wp-includes/images/smilies/icon_razz.gif" alt=":P" class="wp-smiley"> ageStatePersister
02.{
03.public DummyPageStatePersister(Page p) : base(p) { }
04.public override void Load() { }
05.public override void Save() { }
06.}
07.
08.private DummyPageStatePersister _PageStatePersister;
09.protected override PageStatePersister PageStatePersister
10.{
11.get
12.{
13.if (_PageStatePersister == null)
14._PageStatePersister = new DummyPageStatePersister(this);
15.return _PageStatePersister;
16.}
17.}

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