Create new account I forgot my password    

What's New in ASP.NET 4.0 – Better ViewState Control
Rating: 11 user(s) have rated this article Average rating: 3.9
Posted by: Suprotim Agarwal, on 3/14/2010, in category "ASP.NET 2.0 & 3.5"
Views: this article has been read 32042 times
Abstract: In ASP.NET 2.0/3.5, you could disable ViewState for individual controls. However what you could not do is disable ViewState at a Page level and then enable it individually for controls on that page that require it. ASP.NET 4.0 changes that and gives more control to developers.

What's New in ASP.NET 4.0 – Better ViewState Control
 
In ASP.NET 2.0/3.5, you could disable ViewState for individual controls. However what you could not do is disable ViewState at a Page level and then enable it individually for controls on that page that require it. ASP.NET 4.0 changes that and gives more control to developers.
This article is Part II of the multi-part series covering new features on ASP.NET 4.0. I had recently written on What's New in ASP.NET 4.0 – SEO Enhancements with MetaKeywords and MetaDescription properties.
ASP.NET 4.0 introduces the ViewStateMode property that lets you disable ViewState at a Parent level and then enable it for the child controls that require it.
Let’s see this with an example. We will first create a page in ASP.NET 3.5 and disable ViewState on the Parent level and then observe the behavior of ViewState in child controls. We will then use the same example in ASP.NET 4.0 using the ViewStateMode property and observe the changes.
ViewState in ASP.NET 2.0/3.5
 
Create an ASP.NET application. Place two Label controls and one Button control on the page. Now disable view state on the Page by setting the ‘EnableViewState’ property to false. On the first Label control, set ‘EnableViewState’ property to true as shown below:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
EnableViewState="false" 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>ViewState Demo in ASP.NET 3.5</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="One" EnableViewState="true"></asp:Label><br />
        <asp:Label ID="Label2" runat="server" Text="Two"></asp:Label><br /><br />
        <asp:Button ID="Button1" runat="server" Text="PostBack" />
    </div>
    </form>
</body>
</html>
 
In the code behind file, write the following code which set’s the text of the Label controls:
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Label1.Text = "Label1 Changed";
        Label2.Text = "Label2 Changed";
    }
}
 
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
      If (Not IsPostBack) Then
            Label1.Text = "Label1 Changed"
            Label2.Text = "Label2 Changed"
      End If
End Sub
Now what is ‘ideally’ expected out of this setup is that although ViewState is disabled for the entire page, yet Label1 should save ViewState and retain its new value ‘Label1 Changed’ after postback; since ViewState is explicitly enabled on it.
Run the application.
Before PostBack
Postback4-1
After PostBack
Postback1
You will observe that on hitting the button (causing a postback), Label1 does not retain the value (Label1 Changed) explicitly set on it. Not as we expected!
ViewState in ASP.NET 4.0
 
We will now create a similar web application in ASP.NET 4.0. The only difference is that here we will use the new ‘ViewStateMode’ property.
The ViewStateMode property accepts three values: Enabled, Disabled, andInherit.
Enabled - enables view state for that control and any child controls that are set to ‘Inherit’ or that have nothing set.
Disabled - disables view state
Inherit - specifies that the control uses the ViewStateMode setting from the parent control.
The markup looks similar to the following:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
ViewStateMode="Disabled" 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 id="Head1" runat="server">
    <title>View State Demo in ASP.NET 4.0</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="One" ViewStateMode="Enabled"></asp:Label><br />
        <asp:Label ID="Label2" runat="server" Text="Two"></asp:Label> <br /><br />      
        <asp:Button ID="Button1" runat="server" Text="PostBack" />
    </div>
    </form>
</body>
</html>
 
Observe that ViewStateMode on the Page is set to Disabled. The child Label1 control has ViewStateMode set to Enabled, so Label1 ‘ideally’ should save view state. Since the ViewStateMode property is not set on Label2 , so the Label2 control inherits this property value from its parent (Page) and therefore saves no view state.
Note: The default value of ViewStateMode is Enabled for Pageobject. The default value of ViewStateMode is Inherit for controls.
Run the application and hit the button. What do you expect now?
When the page first loads, both the Label controls display the text as set in the codebehind file.
Before PostBack
Postback2-1
However, after hitting the button and causing a postback, Label1 does retain its value as we had expected!
After PostBack
Postback3-1
This is a welcome change as now we can disable ViewState on a parent level and enable it only for those child controls that require it. This improves performance with lesser efforts and gives us more control. This was however just a demo. You will realize maximum advantage of this new feature in a Master-Content page scenario by setting ViewStateMode to Disabled for the Master page and then enable it individually in Content Pages, or individually on controls that require view state.
The entire source code of this article can be downloaded over here.
I hope you liked this article and I thank you for viewing it.
If you liked the article,  Subscribe to the RSS Feed or Subscribe Via Email









Page copy protected against web site content infringement by Copyscape


How would you rate this article?

User Feedback
Comment posted by Jatin Solanki on Monday, April 05, 2010 10:42 AM
Thanks I liked this simpal Article , would like to more of the new changes in .NEt 4.0
Comment posted by zhangronghua on Tuesday, April 06, 2010 8:59 PM
nice article , thanks.
Comment posted by Aditya Nayak on Thursday, April 08, 2010 2:01 AM
nice article!! will like to know about new features in ASP.NET 4.0.
Comment posted by ipomz on Monday, April 26, 2010 3:34 AM
thank
Comment posted by Jonathan Conway on Saturday, May 01, 2010 7:47 AM
ViewState isn't an evil monster. Just a powerful beast that must be tamed. :) [http://jonathanconway.net]
Comment posted by Dilli on Sunday, May 02, 2010 11:49 PM
Thanks for explaining it clear with samples
Comment posted by Veeru on Wednesday, May 05, 2010 2:57 AM
Nice article....
Comment posted by Dhaval on Wednesday, May 05, 2010 5:29 AM
Nice one... Need to know about more articles like this, regarding new features of 4.0
Regards
Comment posted by echo on Thursday, May 06, 2010 3:12 AM
Great! thanks
Comment posted by Satheesh on Monday, May 10, 2010 3:09 AM
Good one..Need to know about more articles like this, regarding new features of 4.0
Comment posted by Najmuddin Saify on Monday, June 21, 2010 2:02 AM
Nice one. Excellent Example really helpful.
Comment posted by Rupesh Tiwari on Sunday, August 29, 2010 6:33 AM
Again a good and important article. keep it up.

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel

NEWSLETTER