Difference between EnableViewState and ViewStateMode properties

ViewState is a very important  for the programmers in page performance point of view. Programmers leave it enable for all the controls even if there is no need to persist their value. Right use of ViewState can increase the performance of the page drastically.

Using EnableViewState property we only have 2 options.
  • We can turn off view state altogether.                 
  • Enable viewstate for the entire page and then turn it off on a control-by-control basis.
If you want to turn of ViewState for the entire page and only enable it for specific controls on the page, then we have to use ViewStateMode property in conjunction with EnableViewState.

EnableViewState property only accepts true or false values and the default value is true, where as ViewStateMode property can have a value of - Enabled, Disabled and inherit. Inherit is the default value for ViewStateMode property, If we set this then it takes the value from the parent and set it accordingly. Suppose If it has been set to Disabled at Page level and for a checkbox you set ViewStateMode = “Inherit” then it will disable the ViewState for the checkbox.

ViewStateMode property is introduced in  4.0, where as EnableViewState exists from a long time.

If EnableViewState is to True, only then the ViewStateMode settings are applied, where as, if EnableViewState is set to False then the control will not save its view state, regardless of the ViewStateMode setting. In short if EnableViewState is set to False, ViewStateMode setting is not respected.

To disable view state for a page and to enable it for a specific control on the page, set the EnableViewState property of the page and the control to true, set the ViewStateMode property of the page to Disabled, and set  the ViewStateMode property of the control to Enabled.
Tags: , ,

Join Us!