Tuesday, August 2, 2011

What's new in ASP.NET 4.0: ViewStateMode

View state is used to persist application data between page requests using client side resources.
If scalability is a major concern over performance, this will be a better mechanism to persist data as storing data in the page itself will reduce the contention on the server, thus allowing more user requests.

However, transfering data between request is costly if you are adding lengthy text to the view state.
So, disabling view state when not required will improve the performance of the application.

You can enable or disable the view state using EnableViewState and ViewStateMode properties in page or control level. Also you can use EnableViewState in web.config to manage it in application level.

How ViewStateMode works?

If you EnableViewState = true in page level, by default ViewStateMode is set as inherit in controls in that page which will enable view state for all the control. By changing the ViewStateMode of the page, you can configure the behaviour for controls as you wish.
However, if you set Page.EnableViewState = false, the view state is disabled no matter the value given in the ViewStateMode. 
You can set Page.ViewStateMode = enabled and Control.EnableViewState = false and Control.ViewStateMode = disabled to disable view state for selected controls.

You can do the vice versa, to disable view state for page and it's controls and then enable view state for selected controls.

No comments:

Post a Comment