Click here to Skip to main content
15,892,298 members
Articles / Web Development / ASP.NET

Keep ASP.NET ViewState out of ASPX Page for Performance Improvement

Rate me:
Please Sign up or sign in to vote.
4.37/5 (46 votes)
23 Jul 2005GPL34 min read 309.1K   1.1K   108  
How you could improve performance of your ASP.NET projects, keeping ViewState on the server instead of on the ASPX page.
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="ServerViewState.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    <HEAD>
        <title>WebForm1</title>
        <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
        <form id="Form1" method="post" runat="server">
            <P>You <STRONG>MUST</STRONG> have the Microsoft <STRONG>NETSDK</STRONG> installed 
                to see this example.</P>
            <P>This form is inherited directly from <STRONG>System.Web.UI.Page</STRONG>. See on 
                the Navigator the size of Its source code.
                <BR>
                <FONT color="#000099"><STRONG>See the Size of the 2 Text Files on the "Generated 
                        SourceCode Examples" Folder</STRONG></FONT>
            </P>
            <P>Use the link below to go to the other page, that is inherited from the VSPage 
                Class<BR>
                <asp:LinkButton id="LinkButton1" runat="server">See next Page (With Server ViewState Enabled)</asp:LinkButton></P>
            <P>
                <asp:DataGrid id="DataGrid1" runat="server" BorderColor="Tan" BorderWidth="1px" BackColor="LightGoldenrodYellow"
                    CellPadding="2" GridLines="None" ForeColor="Black" AutoGenerateColumns="False" Font-Size="X-Small">
                    <FooterStyle BackColor="Tan"></FooterStyle>
                    <SelectedItemStyle ForeColor="GhostWhite" BackColor="DarkSlateBlue"></SelectedItemStyle>
                    <AlternatingItemStyle BackColor="PaleGoldenrod"></AlternatingItemStyle>
                    <HeaderStyle Font-Bold="True" BackColor="Tan"></HeaderStyle>
                    <Columns>
                        <asp:BoundColumn DataField="OrderID" SortExpression="OrderID" HeaderText="OrderID"></asp:BoundColumn>
                        <asp:BoundColumn DataField="CustomerID" SortExpression="CustomerID" HeaderText="CustomerID"></asp:BoundColumn>
                        <asp:BoundColumn DataField="EmployeeID" SortExpression="EmployeeID" HeaderText="EmployeeID"></asp:BoundColumn>
                        <asp:BoundColumn DataField="OrderDate" SortExpression="OrderDate" HeaderText="OrderDate"></asp:BoundColumn>
                        <asp:BoundColumn DataField="ShipName" SortExpression="ShipName" HeaderText="ShipName"></asp:BoundColumn>
                        <asp:BoundColumn DataField="ShipAddress" SortExpression="ShipAddress" HeaderText="ShipAddress"></asp:BoundColumn>
                        <asp:BoundColumn DataField="ShipCity" SortExpression="ShipCity" HeaderText="ShipCity"></asp:BoundColumn>
                    </Columns>
                    <PagerStyle HorizontalAlign="Center" ForeColor="DarkSlateBlue" BackColor="PaleGoldenrod"></PagerStyle>
                </asp:DataGrid></P>
        </form>
    </body>
</HTML>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Web Developer
Brazil Brazil
Régis Daniel is currently living in Itaúna, MG, Brazil. He works with programming since 1999 and actualy works as IT Manager on a wholesaler company. He has experiency as Oracle DBA, and also worked as a PalmOS developer using CodeWarrior with C/C++.
Now a days he works on .NET Framework, developing solutions on Visual Basic.NET and ASP.NET. During his spare time, he likes reading books, watching movies and pratices some outdoor activities, like Trekking and Camping.

Comments and Discussions