ASP.NET 4.0 : ViewStateMode (New Property)
Posted by Virendra Dugar on May 17, 2009
ViewState is a very misunderstood animal. ViewState is a headache for the programmers. 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.
Before I discuss about this new property, let’s come back to the current era of ASP.NET 3.5 or previous version of .NET. What if you turn off ViewState at page level and make it on at control level? Ask yourself, is it possible with previous versions of .NET (upto 3.5)? Sorry but there is a bad news that it’s not possible. But there is a good news as well. You can achieve this with new version of ASP.NET that is 4.0.
ASP.NET 4.0 comes with new and cool property ViewStateMode. It has 3 possible values.
- Enabled : This turns on the ViewState for the control.
- Disabled : This turns off the ViewState for the control.
- Inherit : Takes the value from the parent and set it accordingly. If it has been set to Disabled at Page level and for textbox you set ViewStateMode = “Inherit” then it will disable the ViewState for the textbox.
The main difference between EnableViewState and ViewStateMode property is, if EnableViewState property is set to disabled at parent level, there is no way of making viewstate enable at control level, But this is possible with this new property :ViewStateMode.
It’s really cool and simple.. is n’t it?