Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

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" 
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="ViewStateDemoLebel1"  ViewStateMode="Enabled"  
	runat="server"></asp:Label>
     <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

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>
 <asp:Label ID="ViewStateDemoLebel2"   runat="server"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />     

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.

Summary

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 DotNetKicks.com
Shout it


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

License

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

Share

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 : http://abhijitjana.net
Web Site : http://dailydotnettips.com
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide
Follow on   Twitter

Comments and Discussions

 
GeneralKHUB BHALO LAGCHA as Beginers PinmemberDEBANJAN MANNA16-Apr-12 2:17 
GeneralMy vote of 5 PinmemberDEBANJAN MANNA16-Apr-12 2:15 
GeneralMy vote of 5 PinmemberAnurag Gandhi15-Oct-10 3:22 
GeneralMy vote of 1 PinmemberMember 231714411-Oct-10 0:08 
GeneralNice one PinmentorBrij17-May-10 1:00 
GeneralRe: Nice one PinmvpAbhijit Jana17-May-10 1:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web04 | 2.8.1411023.1 | Last Updated 15 May 2010
Article Copyright 2010 by Abhijit Jana
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid