Difference between EnableViewState and ViewStateMode properties

EnableViewState

1. Property Introduction: EnableViewState exists from a long time

2. Available Values: EnableViewState property only accepts true or false values

3. Default Value: True

4. What EnableViewState does ? To enable ViewState at Page level

5. How to use ? Using EnableViewState property we can have one of these 2 options.

i. We can turn off view state altogether by setting its value to false in web.config file,or

ii.Enable viewstate for the entire page and then turn it off on a control-by-control basis.

ViewStateMode

1. Property Introduction: ViewStateMode property is introduced in ASP.NET 4

2. Available Values: ViewStateMode property can have a value of - Enabled, Disabled and inherit.

3. Default Value: Inherit

4. What ViewStateMode does ? To enable ViewState at Control level

5. How to use ? Steps involved to disable view state for a page and to enable it for a specific control on the page,

i. Set the EnableViewState property of the page and the control to true,

ii. Set the ViewStateMode property of the page to Disabled, and

iii. Set the ViewStateMode property of the control to Enabled.

Summary:

1. If EnableViewState is to True, only then the ViewStateMode settings are applied.

2. In other words, if EnableViewState is set to False, ViewStateMode setting is not respected

3. Therefore, we have to use ViewStateMode property in conjunction with EnableViewState.