Click here to Skip to main content
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
Increasing the performance of your ASP.NET website by reducing the download footprint of your pages.
ServerSide_ViewState.zip
ServerSide_ViewState
App_Browsers
PageStateAdapter.browser
App_Code
App_Data
Cache
Global.asax
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 ViewStateInCache : System.Web.UI.Page
{

    public List<GVDataObject> GVData
    {
        get
        {
            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()
                               });
            }
            BindGridView();
        }
    }

    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()
        });
        BindGridView();
    }

    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;
        GridView1.DataBind();
    }
}

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 Microsoft Public License (Ms-PL)

Share

About the Author

datacop
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.

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 8 Aug 2008
Article Copyright 2008 by datacop
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid