Click here to Skip to main content
11,648,785 members (80,867 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » Howto » Downloads
Add your own
alternative version

Efficient Server-Side View State Persistence

, 8 Aug 2008 Ms-PL 69K 971 61
Increasing the performance of your ASP.NET website by reducing the download footprint of your pages.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ViewStateInPage : System.Web.UI.Page
    public List<GVDataObject> GVData
            if (ViewState["GVDATAOBJECT"] == null) ViewState["GVDATAOBJECT"] = new List<GVDataObject>();
            return ViewState["GVDATAOBJECT"] as List<GVDataObject>;
        set { ViewState["GVDATAOBJECT"] = value; }

    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            for (int i = 0; i < 3; i++)
                GVData.Add(new GVDataObject
                    Field1 = DateTime.Now.ToLongTimeString(),
                    Field2 = DateTime.Now.Ticks.ToString(),
                    Field3 = Path.GetRandomFileName()

    protected void Button1_Click(object sender, EventArgs e)
        DateTime time = DateTime.Now;
        TextBox1.Text = time.ToLocalTime().ToString();
        GVData.Add(new GVDataObject
                           Field1 = time.ToLongTimeString(),
                           Field2 = time.Ticks.ToString(),
                           Field3 = Path.GetRandomFileName()

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType != DataControlRowType.DataRow) return;

        GVDataObject obj = e.Row.DataItem as GVDataObject;

        ((Literal) e.Row.FindControl("Field1")).Text = obj.Field1;
        ((Literal) e.Row.FindControl("Field2")).Text = obj.Field2;
        ((Literal) e.Row.FindControl("Field3")).Text = obj.Field3;

    private void BindGridView()
        GridView1.DataSource = GVData;

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.


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

Team Leader ICAN
United States United States
Jason Monroe is a Project Manager / Architect with the ICAN grant for the Indiana Department of Education. He has been a professional software developer for 13 years, with the last 8 years focusing on Microsoft technologies.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150804.4 | Last Updated 8 Aug 2008
Article Copyright 2008 by datacop
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid