ASP.NET ViewState in VB.NET

View state maintains the state of all asp controls property on the page whenever the page post backs to the web server.
  • 3541
Download Files:
 


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.EventArgsHandles 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 ObjectByVal 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 (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.EventArgsHandles 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 ObjectByVal 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: 

1 (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: 

2 (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 (2).gif

Figure 4

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.