ViewStateMode in ASP.Net 4.0

When asp.net introduced the concept of viewstate, it changed the way how developers maintain the state for the controls in a web page. Until then to keep the track of the control(in classic asp), it was the developer responsibility to manually assign the posted content before rendering the control again. Viewstate made allowed the developer to do it with ease. The developers are not bothered about how controls keep there state on post back.

Viewstate is rendered to the browser as a hidden variable __viewstate. Since viewstate stores the values of all controls, as the number of controls in the page increases, the content of viewstate grows large. It causes some websites to load slowly.

As developers we need viewstate, but actually we do not want this for all the controls in the page. Till asp.net 3.5, if viewstate is disabled from web.config (using <pages viewstate=”false”/> ..</pages>), then you can not enable it from the control level/page level. Both <%@ Page EnableViewState=”true”…. and <asp:textbox EnableViewState=”true” will not work in this case.

Lot of developers demands for more control over viewstate. It will be useful if the developers are able to disable it for the entire page and enable it for only those controls that needed viewstate. With ASP.NET 4.0, this is possible, a happy news for the developers. This is achieved by introducing a new property called ViewStateMode.

Let us see, What is ViewStateMode – Is a new property in asp.net 4.0, that allows developers to enable viewstate for individual control even if the parent has disabled it. This ViewStateMode property can contain either of three values

  1. Enabled- Enable view state for the control even if the parent control has view state disabled.
  2. Disabled - Disable view state for this control even if the parent control has view state enabled
  3. Inherit - Inherit the value of ViewStateMode from the parent, this is the default value.

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

Find the example below.

Page directive - <%@ Page Language="C#"  EnableViewState="True" ViewStateMode="Disabled" .......... %>

Code for the control  - <asp:TextBox runat="server" ViewStateMode="Enabled" ............../>

Now the viewstate will be disabled for the whole page, but enabled for the TextBox.

ViewStateMode gives developers more control over the viewstate.

Published Tuesday, April 06, 2010 9:59 AM by sreejukg

Comments

# re: ViewStateMode in ASP.Net 4.0

Friday, April 09, 2010 7:15 PM by BP

I love this change.  The ViewState, ***carefully used*** saves me so much time.  As long as the ViewState is understood and taken care of I've never had performance issues from it.  This change makes taking care of it easier.  

# Twitter Trackbacks for ViewStateMode in ASP.Net 4.0 - K. G. Sreeju Nair [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 ViewStateMode in ASP.Net 4.0 - K. G. Sreeju Nair         [asp.net]        on Topsy.com

# Dew Drop &ndash; June 28, 2010 | Alvin Ashcraft&#039;s Morning Dew

Pingback from  Dew Drop &ndash; June 28, 2010 | Alvin Ashcraft&#039;s Morning Dew

# re: ViewStateMode in ASP.Net 4.0

Tuesday, July 20, 2010 10:58 PM by Md Shahid

Hi, I am having one doubt in viewstate. If we disabled the viewstate at page level and control level , then also the value persist in control after postback. How and why this behaviour is happens.

Regards

Shahid

# re: ViewStateMode in ASP.Net 4.0

Wednesday, July 28, 2010 2:38 AM by sreejukg

Server controls will load its values from request object. That is how it keeps track of the values.

refer the below URL (See the section Programming Without View State)

msdn.microsoft.com/.../cc188774.aspx

# Managing View State in ASP.NET 4 Using the New ViewStateMode Property

Saturday, August 14, 2010 4:31 AM by ASP.NET Web Forms bloggers

The ASP.NET Web Forms model strives to encapsulate the lower level complexities involved in building

Leave a Comment

(required) 
(required) 
(optional)
(required)