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 6, 2010 9:59 AM by sreejukg

Comments

# re: ViewStateMode in ASP.Net 4.0

Friday, April 9, 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.  

# re: ViewStateMode in ASP.Net 4.0

Tuesday, June 29, 2010 11:47 AM by the_dancer

*dances a jig*

# 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

# re: ViewStateMode in ASP.Net 4.0

Monday, September 13, 2010 1:49 AM by Ajit

A very Nice Article.....

# re: ViewStateMode in ASP.Net 4.0

Wednesday, December 15, 2010 7:39 PM by Rez

I am just wondering why the hidden input field is still generated when the page level EnableViewState is false and ViewStateMode is set to disabled for the page and everything on the page.

# re: ViewStateMode in ASP.Net 4.0

Saturday, December 25, 2010 11:21 PM by Risa

I have set these to the page:

EnableViewState="false" ViewStateMode="Inherit" EnableEventValidation="false"

Also explicitly set EnableViewState="false" to each server control on the page. But still the control values are retained in post back. The rendered page source got the hidden field __VIEWSTATE with a value.

I am trying to understand what's going on under the hood. Can anyone please explain?

# re: ViewStateMode in ASP.Net 4.0

Thursday, September 29, 2011 8:40 AM by amitava

very helpful article....good job.

# re: ViewStateMode in ASP.Net 4.0

Wednesday, December 28, 2011 7:13 AM by Suresh Kadam

Thank you, explanation is abstract and clear.

# re: ViewStateMode in ASP.Net 4.0

Thursday, February 23, 2012 9:44 PM by jovita

very informative..and concept clearing article .

# re: ViewStateMode in ASP.Net 4.0

Tuesday, May 1, 2012 1:36 PM by oem software

QedoJ1 Say, you got a nice blog article.Really looking forward to read more. Want more.

# re: ViewStateMode in ASP.Net 4.0

Wednesday, May 16, 2012 4:31 PM by tWhRLEPRujScgcgIPnl

9s1cnU Thanks a lot for the post. Will read on...