What's New in ASP.NET 4.0 – Better ViewState Control
Posted by: Suprotim Agarwal ,
on 3/14/2010,
in
Category ASP.NET
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.
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
After PostBack
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
However, after hitting the button and causing a postback, Label1 does retain its value as we had expected!
After PostBack
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.
This article has been editorially reviewed by Suprotim Agarwal.
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 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 Book 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 the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) 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!
Was this article worth reading? Share it with fellow developers too. Thanks!
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 Sixteen consecutive years. 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