Click here to Skip to main content
15,881,803 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 301K   1.1K   108  
How you could improve performance of your ASP.NET projects, keeping ViewState on the server instead of on the ASPX page.
Imports System.Data.SqlClient

Public Class WebForm1
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents LinkButton1 As System.Web.UI.WebControls.LinkButton
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Dim myConnection As SqlConnection

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Put user code to initialize the page here
        myConnection = New SqlConnection(" DATABASE=Northwind;SERVER= localhost;UID=sa;PWD=password;")
        If Not Page.IsPostBack Then BindGrid()
    End Sub

    Private Sub BindGrid()

        'Create a DataAdapter to fetch data from Products Table
        Dim myCommand As SqlDataAdapter
        myCommand = New SqlDataAdapter("select * from orders WHERE ORDERID < 10600", "DATABASE=Northwind;SERVER=localhost;UID=sa;PWD=;")

        'Create a DataSet and Fills it with Product Data
        Dim ds As New DataSet
        myCommand.Fill(ds, "OrderID")

        DataGrid1.DataSource = ds.Tables(0)
        DataGrid1.DataBind()

    End Sub

    Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
        Response.Redirect("WebForm2.aspx")
    End Sub
End Class

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