What's New in ASP.NET 4.0 – Better ViewState Control

Posted by: Suprotim Agarwal , on 3/14/2010, in Category ASP.NET
Views: 100414
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

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce the The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on .NET Standard and the upcoming C# 8.0 too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for ten consecutive times. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Jatin Solanki on Monday, April 5, 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 6, 2010 8:59 PM
nice article , thanks.
Comment posted by Aditya Nayak on Thursday, April 8, 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 1, 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 2, 2010 11:49 PM
Thanks for explaining it clear with samples
Comment posted by Veeru on Wednesday, May 5, 2010 2:57 AM
Nice article....
Comment posted by Dhaval on Wednesday, May 5, 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 6, 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.
Comment posted by rajesh on Friday, October 1, 2010 1:57 PM
very nice....
Comment posted by Sharad K on Monday, November 22, 2010 8:07 AM
Perfect!!!
Comment posted by faheem on Saturday, April 2, 2011 12:30 AM
By serving less time got more ... good
Comment posted by tech.abi on Saturday, January 14, 2012 12:55 PM
ASP.NET lovers check this URL:
http://tech-records.blogspot.com

This blog describes new features of ASP.NET 4.0(Session State Compression,ViewState,URL Routing,
Output Cache Extesibility, diff between ASP.NET 4.0 and its previous version) and the BASICS of
ASP.NET(Common Language Runtime,Common Type System,Common Language Specification, Class Library,
FCL)
Comment posted by Nitin Tyagi on Saturday, February 11, 2012 3:28 AM
nice
Comment posted by Durgaprasad chanda on Thursday, May 31, 2012 2:13 AM
this is veru useful for begineers
Comment posted by Naiomi on Monday, July 2, 2012 6:45 AM
Thanks for this article, really helpful for helping understand my issue of viewstate being encoded into the URL.

Just a tip: if you are having the same issue of viewstate being encoded into the url, just make sure the <form> tag doesn't include any GET or POST methods:

DON'T do this - <form id = "someid" runat="server" method="GET|POST">

Instead,

DO this - <form id = "someid" runat="server">

Thanks :)
Comment posted by DK Soni on Thursday, August 9, 2012 7:22 AM
Really an ilu-ilu feature........
Comment posted by Arun Singh RAwat on Wednesday, January 23, 2013 12:48 AM
It's very helpfull for knowing view state. and also for understand difference.
Comment posted by Arun Singh RAwat on Wednesday, January 23, 2013 12:48 AM
It's very helpfull for knowing view state. and also for understand difference.

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

C# .NET BOOK

C# Book for Building Concepts and Interviews

Tags

JQUERY COOKBOOK

jQuery CookBook