Loading ...

ViewState Management in ASP.NET 4.0 | CodeAsp.Net

ViewState Management in ASP.NET 4.0

(2890)
0
/5
Avg: 0/5: (0 votes)
Published: 6/3/2010 by Samir NIGAM

Introduction

As we know ViewState setting in ASP.NET has a hierarchical nature i.e. if the ViewState is disabled on the parent control; it cannot be enabled on any of its child controls. In other words Parent level ViewState setting has highest priority i.e. if we disable Parent level ViewState through EnableViewState= “False”, we can’t enable ViewState for any child control even if we try to set EnableViewState= “True” for child control. Now ASP.NET 4.0 has solved this problem.

ViewState in ASP.NET 4.0

ASP.NET 4.0 introduces a property ViewStateMode; with the help of it we can disable ViewState at a Parent level and can enable it for child control. In ASP.NET 4.0 ViewStateMode property accepts following values-

ValueDescriptionRemark
EnabledThis will enable View State for particular control and also for any child control which have ‘Inherit’ value for this property or nothing set for this property.Default for Page object.
DisabledThis will disable View State for that particular control. 
InheritThis will specify that control will use the parent control ViewStateMode property.Default for server controls.

 Let’s understand it.

Demo

Simply creates a demo web application with following HTML markup-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sample.aspx.cs" Inherits="Sample" %>

<!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="lblDemo" runat="server" ></asp:Label>
    <asp:Button ID="btnPostback" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>


Put following code in Page_Load method-

 

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            lblDemo.Text = DateTime.Now.ToString();
        }
    }


Lets discuss some cases.

Case I

 Page Control
ViewStateMode Enabled Disabled
<%@ Page Language="C#" ViewStateMode="Enabled" AutoEventWireup="true" CodeFile="Sample.aspx.cs" Inherits="Sample" %>

<asp:Label ID="lblDemo" runat="server" ViewStateMode="Disabled" ></asp:Label>


disabled

In this case ViewState does not persist after the post back showing that control’s ViewState is independent of its parent’s ViewState.

 Case II

 Page Control
ViewStateMode DisabledEnabled 
<%@ Page Language="C#" ViewStateMode="Disabled" AutoEventWireup="true" CodeFile="Sample.aspx.cs" Inherits="Sample" %>

<asp:Label ID="lblDemo" runat="server" ViewStateMode="Enabled" ></asp:Label>

Enabled
In this case ViewState persists after the post back again showing that control’s ViewState is independent of its parent’s ViewState.

Case III

 Page Parent Control
ViewStateMode DisabledEnabled Inherit
<%@ Page Language="C#" ViewStateMode="Disabled" AutoEventWireup="true" CodeFile="Sample.aspx.cs" Inherits="Sample" %>

<asp:Panel ID="pnlParent" runat="server" ViewStateMode="Enabled" >
	<asp:Label ID="lblDemo" runat="server" ViewStateMode="Inherit" ></asp:Label>
    </asp:Panel>

Enabled
In this case ViewState persists after post back again as child control inherits its parent’s ViewStateMode.

Conclusion

So we have seen that we can set child control’s ViewStateMode irrespective of parent’s ViewStateMode or can inherits parent’s ViewStateMode in very simple manner. It’s a very good feature in ASP.NET 4.0. This feature improves performance of the page with lesser pains and provides more control.

 

Comments (no comments yet)

  • Name:*
  • Email:*
  • Website:
Type the characters you see in the image *

Top articles

Quick Vote

What kind of email newsletter would you prefer to receive from CodeAsp.Net?