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

  • This article is Part II of the multi-part series covering new features on ASP.NET 4.0.
  • 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.
  • 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:
   1: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" EnableViewState="false" Inherits="_Default" %>

   2:  

   3: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

   4:  

   5: <html xmlns="http://www.w3.org/1999/xhtml">

   6: <head runat="server">

   7:     <title>ViewState Demo in ASP.NET 3.5</title>

   8: </head>

   9: <body>

  10:     <form id="form1" runat="server">

  11:     <div>

  12:         <asp:Label ID="Label1" runat="server" Text="First" EnableViewState="true"></asp:Label><br />

  13:         <asp:Label ID="Label2" runat="server" Text="Second"></asp:Label><br /><br />

  14:         <asp:Button ID="Button1" runat="server" Text="PostBack" />

  15:     </div>

  16:     </form>

  17: </body>

  18: </html>

In the code behind file, write the following code which set’s the text of the Label controls:

   1: if(!IsPostBack)

   2: {

   3:     Label1.Text = "My First Example";

   4:     Label2.Text = "My Second Example";

   5: }

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.

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:

   1: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" ViewStateMode="Disabled" Inherits="_Default" %>

   2:  

   3: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

   4:  

   5: <html xmlns="http://www.w3.org/1999/xhtml">

   6: <head id="Head1" runat="server">

   7:     <title>View State Demo in ASP.NET 4.0</title>

   8: </head>

   9: <body>

  10:     <form id="form1" runat="server">

  11:     <div>

  12:         <asp:Label ID="Label1" runat="server" Text="First" ViewStateMode="Enabled"></asp:Label><br />

  13:         <asp:Label ID="Label2" runat="server" Text="Second"></asp:Label> <br /><br />      

  14:         <asp:Button ID="Button1" runat="server" Text="PostBack" />

  15:     </div>

  16:     </form>

  17: </body>

  18: </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.

However, after hitting the button and causing a postback, Label1 does retain its value as we had expected!

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s