Archive

Posts Tagged ‘ASP.NET’

ASP.NET: Disabling ViewState from a DataGrid

December 26, 2007 Simeon Lobo Leave a comment

For performance reasons, I prefer to disable ViewState in all ASP.NET controls unless I explicitly need to. This arises from my desire to keep web page HTML payload to 15KB for sub-second response times (a personal UI design goal).

 

/// <summary>

/// Class member used to disable ViewState at the DataGridItem level. This method must be

/// called after every databinding event

/// </summary>

/// <param name=”dg”></param>

public void DisableViewState(System.Web.UI.WebControls.DataGrid dg)

{

    foreach (System.Web.UI.WebControls.DataGridItem dgi in dg.Items)

    {

        dgi.EnableViewState = false;

    }

}

/// <summary>

/// Sets the selected item for a control. Use this to reset listboxes or dropdown

/// boxes and minimise the use of ViewState between PostBacks

/// </summary>

/// <param name=”lc”>The instance of the Control that we wish to databind</param>

public void SetSelectedItem(System.Web.UI.WebControls.ListControl lc)

{

    String val = Request.Form[lc.ID];

    if (val != null)

        for (int i = 0; i < lc.Items.Count; i++)

            if (lc.Items[i].Value == val)

            {

                lc.Items[i].Selected = true;

                return;

            }

}

 

Categories: Technical Tags: , ,