Click here to Skip to main content
11,922,381 members (31,325 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


21 bookmarked

ViewState Control in ASP.NET 4.0

, 15 May 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
ViewState Control in ASP.NET 4.0

Viewstate is one of the most important and useful client side state management mechanisms. It can store the page value at the time of post back (Sending and Receiving information from Server) of your page. ASP.NET pages provide the View State property as a built-in structure for automatically storing values between multiple requests for the same page.

We generally use “EnableViewState” Properties for both Page Level and Server Control Level to maintain the view state. Till ASP.NET 3.5 Version, Page Level view state control treat as highest priorities. Which means If we set EnableViewState= “False” in page level, that will automatically be derived by all the server side controls. In that case, if we set “EnableViewState=”True”” for any server side control will treat it as false, as we have defined them “False” in Page Level. Here is one complete article on ASP.NET 2.0/3.5 View State, which may be helpful for you.

Now, let’s have a look into the changes in ViewState Control in ASP.NET 4.0. There is a massive change in View State Control in ASP.NET 4.0 which is very much helpful for the developer also. ASP.NET 4.0 added a new property to Page object and server controls called ViewStateMode. ViewStateMode properties has 3 values:

  1. Enabled: This value will enable the view state for page level or control level. This is the default value for the Page object.
  2. Disabled: This value will disable the viewstate for both page level and control level.
  3. Inherit: This value will make the control inherit the setting of the parent. This is the default value for the server control.

These properties and their behaviors indicate the server control viewstates are totally independents on Page Level View State. Which means, ViewStateMode=”Disabled” will disable the viewstate for all the controls for those pages by default, but if we explicitly specify the page level control viewstatemode= ”Enabled”, then only that control viewstate will be maintained during the postback. Let’s check few scenarios of ViewStateMode in ASP.NET 4.0.

We have a sample ASP.NET web application and code snippet is given below:

<%@ Page Language="C#"  ViewStateMode="Disabled" AutoEventWireup="true" 
	CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
    <asp:Label ID="ViewStateDemoLebel1"  ViewStateMode="Enabled"  
     <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

In code behind:

protected void Page_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)
            ViewStateDemoLebel1.Text = "Default Value of Control";

Now, let’s explore the scenarios of ViewStateMode.

Scenario 1: Both Page and Control Level ViewStateMode=”Disabled”

<%@ Page Language="C#"  ViewStateMode="Disabled" AutoEventWireup="true" 
	CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Label ID="ViewStateDemoLebel1"  ViewStateMode="Disabled" runat="server"></asp:Label>

If we have ViewStateMode Disabled for both the page and control level and we run the program, first time label text will be “Default Value of Control” . But After Click on Button1, means after postback “ViewStateDemoLebel” value will be blank. Because we have disabled the viewstate mode properties for both control and page level.

Scenario 2: Page Level ViewStateMode=”Disabled” and Control Level ViewStateMode=”Enabled”

<%@ Page Language="C#"  ViewStateMode="Disabled" AutoEventWireup="true" 
	CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Label ID="ViewStateDemoLebel1"  ViewStateMode="Enabled"  runat="server"></asp:Label>

In this case, after PostBack ViewStateDemoLebel1 value will be the “Default Value of Control” as ViewStateMode is set to Enabled. So, in this point we can understand that page level viewstatemode is totally independent on the server control level ViewStateMode.

Scenario 3: Page Level ViewStateMode=”Enabled” and Control Level ViewStateMode=”Disabled”

To demonstrate the scenario, I have added one more label control named ViewStateDemoLebel2 in the aspx page and default value of that control is “Default Value of Control 2”. Below is the code snippet for that aspx page.

<asp:Label ID="ViewStateDemoLebel1"  ViewStateMode="Disabled" runat="server"></asp:Label>

Here, PageLevel ViewStateMode is enabled, and we explicitly defined ViewStateMode=”Disabled” for ViewStateDemoLebel1. As a result, after post back we have the value of control 2.

Scenario 4: Page Level ViewStateMode=”Enabled” and Control Level ViewStateMode=”Inherited”

<%@ Page Language="C#"  ViewStateMode="Enabled" AutoEventWreup="true" 
	CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Label ID="ViewStateDemoLebel1" ViewStateMode="Inherited"    
	ViewStateMode="Enabled"  runat="server"></asp:Label>

In this case, child control automatically inherits the properties of page level. So for ViewStateDemoLebel1 control viewstatemode will be “enabled” as it used “Inherit” properties and Page Level ViewStateMode is set to enabled.


In this article, we have seen how ViewStateMode properties can be used in ASP.NET 4.0 to take a better control on PageLevel and control Level View State. I have explained most of the scenarios of ViewStateMode. Hope you have enjoyed the article. Cheers!

kick it on
Shout it

Filed under: .NET 4.0, ASP.NET 4.0, My Articles, Visual Studio 2010


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Abhijit Jana
Technical Lead
India India
.NET Consultant | Former Microsoft MVP - ASP.NET | CodeProject MVP, Mentor, Insiders| Technology Evangelist | Author | Speaker | Geek | Blogger | Husband

Blog :
Web Site :
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide

You may also be interested in...

Comments and Discussions

GeneralKHUB BHALO LAGCHA as Beginers Pin
DEBANJAN MANNA16-Apr-12 2:17
memberDEBANJAN MANNA16-Apr-12 2:17 
GeneralMy vote of 5 Pin
DEBANJAN MANNA16-Apr-12 2:15
memberDEBANJAN MANNA16-Apr-12 2:15 
GeneralMy vote of 5 Pin
Anurag Gandhi15-Oct-10 3:22
memberAnurag Gandhi15-Oct-10 3:22 
GeneralMy vote of 1 Pin
Member 231714411-Oct-10 0:08
memberMember 231714411-Oct-10 0:08 
GeneralNice one Pin
Brij17-May-10 1:00
mentorBrij17-May-10 1:00 
GeneralRe: Nice one Pin
Abhijit Jana17-May-10 1:17
mvpAbhijit Jana17-May-10 1:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151120.1 | Last Updated 15 May 2010
Article Copyright 2010 by Abhijit Jana
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid