Custom Search Tailored for Microsoft .NET  
Aspexception.com Skip Navigation Links
Error RepositoryExpand Error Repository
.NET ResourcesExpand .NET Resources
About
Skip Navigation LinksHome > .NET Resources > How to tips... > View State Size
How to display the view state size in ASP.NET
A Web application is stateless. View state is the method that the ASP.NET page framework uses by default to preserve page and control values between round trips. When the HTML for the page is rendered, the current state of the page and values that need to be retained during postback are serialized into base64-encoded strings and output in the view state hidden field or fields.

View state information is serialized into XML and then encoded using base64 encoding, which can generate large amounts of data. When the page is posted back to the server, the contents of view state are sent as part of the page post-back information. If view state contains a large amount of information, it can affect performance of the page. Therefore, it is recommended that you test the performance of your pages using typical data for your application to determine if the size of view state is causing performance problems for your application.

In case you think the view state is too large, you might want to try the alternatives. See ASP.NET State Management Recommendation. The following example shows how to display the size of view state:
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        body1.Attributes["onload"] = "alert('ViewState: ' + document.forms[0]['__VIEWSTATE'].value.length + ' bytes');";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body id="body1" runat="server">
    <form id="form1" runat="server">
    <div>
         <asp:TreeView ID="TreeView1" runat="server">
          <Nodes>
            <asp:TreeNode Text="My Computer">
              <asp:TreeNode Text="Favorites">
                <asp:TreeNode Text="News">  
                  <asp:TreeNode Text="MSN" NavigateUrl="http://www.msn.com"/>
                  <asp:TreeNode Text="MSNBC News" NavigateUrl="http://www.msnbc.msn.com"/>
                </asp:TreeNode>
                <asp:TreeNode Text="Technology">  
                  <asp:TreeNode Text="Microsoft" NavigateUrl="http://www.microsoft.com"/>
                  <asp:TreeNode Text="ASP.NET" NavigateUrl="http://www.asp.net"/>
                  <asp:TreeNode Text="GotDotNet" NavigateUrl="http://www.gotdotnet.com"/>
                  <asp:TreeNode Text="MSDN" NavigateUrl="http://msdn.microsoft.com"/>
                </asp:TreeNode>
                <asp:TreeNode Text="Shopping">  
                  <asp:TreeNode Text="MSN Shopping" NavigateUrl="http://shopping.msn.com"/>
                  <asp:TreeNode Text="MSN Autos" NavigateUrl="http://autos.msn.com"/>
                </asp:TreeNode>
              </asp:TreeNode>
              <asp:TreeNode Text="City Links">
                <asp:TreeNode Text="MapPoint" NavigateUrl="http://www.mappoint.com"/>
                <asp:TreeNode Text="MSN City Guides" NavigateUrl="http://local.msn.com"/>
              </asp:TreeNode>
              <asp:TreeNode Text="Music Links">
                <asp:TreeNode Text="MSN Music" NavigateUrl="http://music.msn.com"/>
              </asp:TreeNode>
            </asp:TreeNode>
          </Nodes>
        </asp:TreeView>   
    </div>
    </form>
</body>
</html>
Site Map  Copyright © Aspexception.com 2008. All rights Reserved. Terms of Use