ARTICLE

ViewState in ASP.NET

Posted by Gurjeet Singh Articles | ASP.NET using VB.NET June 27, 2011
View state maintains the state of all asp controls property on the page whenever the page post backs to the web server.
Download Files:
 
Reader Level:

View state maintains the state of all asp controls property on the page whenever the page post backs to the web server.
By default, view sate is enabled for every asp control. You can see View state in source view of a page and it is saved in hidden form field on the page. View state is good to maintain but it has also disadvantage that it stuffing too much data into view state and it slow down the process of rendering page.

You can maintain or disable individual control, page or an application.
By default EnableViewState="True" for control, page or application.

Disable individual control using Control.EnableViewState property with false.

Use page directive to disable view state on page.
<%@ Page Language="VB" EnableViewState="false" %>
If you set EnableViewState property value false in page directive, then you cannot enable view state property in control level (see the below code)

Code:

Default.aspx

<%@ Page Language="VB" AutoEventWireup="false" EnableViewState="false"  CodeFile="Default.aspx.vb" 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="Label1" runat="server" Text="With View state"></asp:Label>        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label2" runat="server" Text="0"></asp:Label>
        <br />
        <asp:Label ID="Label3" runat="server" Text="Without View state"></asp:Label>
        &nbsp;<asp:Label ID="Label4" runat="server" Text="0" EnableViewState="False"></asp:Label>
        <br />
        <asp:Label ID="Label5" runat="server" ViewStateMode="Enabled"></asp:Label>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Click Here !!!!" 
            onclick="Button1_Click1" />
    </div>
    </form>
</body>
</html>

Default.aspx.vb

Partial
Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Label5.Text = "Page is not post back"
        Else
            Label5.Text = "Page is post back"
        End If
    End Sub
    Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As EventArgs)
        Label2.Text = (Int32.Parse(Label2.Text) + 1).ToString()
        'EnableViewState at Column level
        Label2.EnableViewState = True
        Label4.Text = (Int32.Parse(Label4.Text) + 1).ToString()
        'EnableViewState at Column level
        Label4.EnableViewState = False
    End Sub
End Class

Output:

1.gif

Figure 1

If you set the EnableViewState property value True in page directive, then all control enable its view state property.
<%@ Page Language="VB" AutoEventWireup="false" EnableViewState="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>

Code:

Default.aspx

<%@ Page Language="VB" AutoEventWireup="false" EnableViewState="true" CodeFile="Default.aspx.vb" 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="Label1" runat="server" Text="With View state"></asp:Label>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label2" runat="server" Text="0"></asp:Label>
        <br />
        <asp:Label ID="Label3" runat="server" Text="Without View state"></asp:Label>
        &nbsp;<asp:Label ID="Label4" runat="server" Text="0" EnableViewState="False"></asp:Label>
        <br />
        <asp:Label ID="Label5" runat="server" ViewStateMode="Enabled"></asp:Label>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Click Here !!!!" 
            onclick="Button1_Click1" />
   </div>
    </form>
</body>
</html>

Default.aspx.vb

Partial
Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Label5.Text = "Page is not post back"
        Else
            Label5.Text = "Page is post back"
        End If
    End Sub
    Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As EventArgs)
        Label2.Text = (Int32.Parse(Label2.Text) + 1).ToString()
        'EnableViewState at Column level
        Label2.EnableViewState = True
        Label4.Text = (Int32.Parse(Label4.Text) + 1).ToString()
        'EnableViewState at Column level
        Label4.EnableViewState = False
    End Sub
End Class

Outpu
t:

2.gif

Figure 2

Disable or Enable view state using web configuration file.

You can disable application by setting the <pages EnableViewState="False"/> in web.config file.

web.config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <pages enableViewState="false"/>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0"/>
  </system.web>
</configuration>

Output:

1.gif

Figure 3

Web.config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <pages enableViewState="true"/>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0"/>
  </system.web>
</configuration>

Output:

2.gif

Figure 4

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Team Foundation Server Hosting
Become a Sponsor