View State is one of the most important and useful client side state management mechanisms. It can store the page value at the time of post back (Sending and Receiving information from Server) of your page. ASP.NET pages provide the View State property as a built-in structure for automatically storing values between multiple requests for the same page.

we generally used “EnableViewState” Properties for both Page Level and Server Control Level to maintain the view state. Till ASP.NET 3.5 Version, Page Level view state control treat as highest priorities. Which means If we set EnableViewState= “False” in page level that will automatically derived by all the server side control. In that case if we set “EnableViewState=”True”” for any server side control will treat as false, as we have defined them “False” in Page Level.Here is one complete article on ASP.NET 2.0/3.5 View State , which may helpful for you

Now, let’s have a look into the changes in ViewState Control in ASP.NET 4.0. There is a massive change in View State Control in ASP.NET 4.0 which is very much helpful for developer also. Asp.net 4.0 added a new property to Page object and server controls called ViewStateMode. ViewStateMode properties having 3 values

1. Enabled : This value will enable the view state for page level or control level. This is the default value for the Page object.

2. Disabled: This value will disable the viewstate for both page level and Control Level

3. Inherit : This value will make the control to inherit the setting of the parent. This is the default value for the server control.

These properties and their behaviors indicate the server control viewstates are totally independents on Page Level View State. Which means, ViewStateMode=”Disabled” will disabled the viewstate for all the control for that pages by default, but if we explicitly specify the page level control viewstatemode= ”Enabled”, then only that control viewstate will be maintained during the post back. Let’s check few scenarios of ViewStateMode in ASP.NET 4.0

We have a sample asp.net web application and code snippet is given below.

<%@ Page Language="C#"  ViewStateMode="Disabled" AutoEventWireup="true" CodeFile="Default.aspx.cs" 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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="ViewStateDemoLebel1"  ViewStateMode="Enabled"  runat="server"></asp:Label>
     <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

In Code Behind :

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewStateDemoLebel1.Text = "Default Value of Control";
        }
    }

Now, let’s explore the scenarios of ViewStateMode

Scenario 1 : Both Page and Control Level ViewStateMode=”Disabled”

<%@ Page Language="C#"  ViewStateMode="Disabled" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Label ID="ViewStateDemoLebel1"  ViewStateMode="Disbaled"  runat="server"></asp:Label>

If we have ViewStateMode Disabled for both the page and control level and we run the program, first time label text will be “Default Value of Control” . But After Click on Button1, means after postback “ViewStateDemoLebel” value will be Blank. Because we have disabled the viewstate mode properties for both control and page level.

Scenario 2 : Page Level ViewStateMode=”Disabled” and Control Level ViewStateMode=”Enabled”

<%@ Page Language="C#"  ViewStateMode="Disabled" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Label ID="ViewStateDemoLebel1"  ViewStateMode="Enabled"  runat="server"></asp:Label>

In this case, after PostBack ViewStateDemoLebel1 value will be the “Default Value of Control” as ViewStateMode set to Enabled. So, in this point we can understnad that page level viewstatemode is totally independent on the server control level ViewStateMode.

Scenario 3 : Page Level ViewStateMode=”Enabled” and Control Level ViewStateMode=”Disabled”

To demonstrate the scenario, I have added one more label control named ViewStateDemoLebel2 in aspx page and default value of that control is “Default Value of Control 2”. Below is the code snippet for that aspx page.

<asp:Label ID="ViewStateDemoLebel1"  ViewStateMode="Disabled"  runat="server"></asp:Label>
 <asp:Label ID="ViewStateDemoLebel2"   runat="server"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
     

Here, PageLevel ViewStateMode is enabled, and we explicitly defined ViewStateMode=”Disabled” for ViewStateDemoLebel1. so as a result, after post back we have the value of control 2.

Scenario 4 : Page Level ViewStateMode=”Enabled” and Control Level ViewStateMode=”Inherited”

<%@ Page Language="C#"  ViewStateMode="Enabled" AutoEventWreup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Label ID="ViewStateDemoLebel1" ViewStateMode="Inherite"    ViewStateMode="Enabled"  runat="server"></asp:Label>

In this case, child control automatically ineherite the properties of page level. So for ViewStateDemoLebel1 control viewstatemode will be “enabled” as it used “Inherit” properties and Page Level ViewStateMode Set to enabled.

Summary :

In this article we have seen How ViewStateMode properties can be used in ASP.NET 4.0 to take a better control on PageLevel and control Level View State. I have explained most of the scenarios of ViewStateMode. Hope you have enjoyed the article. Cheers!

kick it on DotNetKicks.com
Shout it