Investigating EnableViewState and ViewStateMode in .NET 4.0

Today, I stumbled upon a problem with enabling a view state for a control.  It took me about 2 hours to fully comprehend the scope of the problem and to find the fix. I hope that sharing my findings might actually help out somebody.

In .NET 4.0 there are two properties that control the ViewState.
ViewStateEnabled (true or false) and ViewStateMode (Enabled, Disabled, Inherit).
ViewStateEnabled can be set for the whole application in a web.config

<pages enableViewState="true|false" />

I used an asp dropdown list on my page and I needed to get the selected value upon the post back. Sounds simple and straightforward, as long as the pages “enableViewState” property is set to true (it is by default).

Let’s start an experiment and create a web application with two pages. Both pages host a dropdown list and the first objective is to retrieve the value of the selected item upon the postback. To make things not trivial set this in the web.config<pages enableViewState="false" />. The pages are almost identical but one of them is a page with a Master and the other is Master less.

We want the pages look like this:

Postback with Master Page

Postback without Master Page

And this is how they start:

Empty Postback with Master Page

Empty Postback without Master Page

In case of the Page without the Master enabling ViewState on the page level is the quickest way to make the ViewState work for the control.

<%@ Page Language="C#" AutoEventWireup="true" 
    CodeBehind="NoMasterPage.aspx.cs" 
    Inherits="ViewStateApp.NoMasterPage" 
    EnableViewState="true" %>

 

In a case of the Page with a Master Page, enabling the ViewState on the page level is not enough. Setting this:

<%@ Page Title="Home Page" Language="C#" 
    MasterPageFile="~/Site.master" 
    AutoEventWireup="true" EnableViewState="true"
    CodeBehind="Default.aspx.cs" 
    Inherits="ViewStateApp._Default" %>

 

will not make the control ViewState work. But enabling the ViewState on the Master Page as well, will do the trick and make the ViewState work for the dropdown list.

<%@ Master Language="C#" AutoEventWireup="true" 
CodeBehind="Site.master.cs" Inherits="ViewStateApp.SiteMaster" 
EnableViewState="true"  %>

 

While both solution are quick. They bloat the site of the pages because the view state gets enabled for all controls. So, now let’s move  to an objective number two. Make the ViewState work for the dropdown lists but keep the ViewState as small as possible.

This is where the ViewStateMode comes to the rescue.

The Page directive for a  No Master Page and the control settings should look like this:

<%@ Page Language="C#" AutoEventWireup="true" 
    CodeBehind="NoMasterPage.aspx.cs" 
    Inherits="ViewStateApp.NoMasterPage" 
    EnableViewState="true" ViewStateMode="Disabled" %>
 <asp:dropdownlist runat="server" ID="ddlItems" EnableViewState="true" ViewStateMode="Enabled"  >

 

The page directive, the Master Page directive and the control settings should look like this for the Page with the Master:

<%@ Page Title="Home Page" Language="C#" 
    MasterPageFile="~/Site.master" 
    AutoEventWireup="true" EnableViewState="true"
    ViewStateMode="Disabled"
    CodeBehind="Default.aspx.cs" 
    Inherits="ViewStateApp._Default" %>

 

<%@ Master Language="C#" AutoEventWireup="true" 
CodeBehind="Site.master.cs" Inherits="ViewStateApp.SiteMaster" 
EnableViewState="true"
ViewStateMode="Disabled"  %>

 

  <asp:dropdownlist runat="server" ID="ddlItems" 
  EnableViewState="true" ViewStateMode="Enabled">
  </asp:dropdownlist>

 

Hope it helps. Just remember about the Master Page when considering how ViewState settings are inherited.

This entry was posted in .NET, ViewState and tagged , . Bookmark the permalink.