Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Performance of ASP.NET Forms Page

0.00/5 (No votes)
24 Jul 2012 1  
Improving page performance.

Introduction

In most web applications there are some occasions where we have put many components and we ended up having performance problems. How do we do it? We harm usability by putting most of the functionality distributed across multiple pages. We always have to look for alternatives to solve customer problems, then we decided to solve it.

History 

In a project I had this problem. A page was getting huge, it had a treeview which was filled by a table (recursion) very large and that the client asked me explicitly that it come with all items expanded. If the case were to be completed and was not expanded, then there would not have been performance issues and we would not have discovered an excellent way to improve the performance of pages.

To know the page size, right click and choose the menu option "Properties". 

Solution of the Problem  

There are two steps to be followed to improve the page: VIEWSTATE stored in session and compress the HTML page.

To store the VIEWSTATE in session, we must override two methods of the page. Then you should add the source code below: 

bool _viewStateInSession = true;

public bool ViewStateInSession
{
    get { return _viewStateInSession; }
    set { _viewStateInSession = value; }
}

#region [ Salvar ViewState em Session ]
// ------------------------------------------------ //
// Estes dois Métodos são sobre-escritos para       //
// melhora no desempenho da aplicação. Pois como    //
// tem muitos dados/controles que tem a             //
// necessidade de serem persistidos (gravados em    //
// viewstate).                                      //
// ------------------------------------------------ //

string key_viewState
{
    get
    {
        return "VIEWSTATE" + url_currentPage;
    }
}

/// <summary>
/// Lê o viewstate da Variável de Sessão
/// </summary>
/// <returns></returns>
protected override object LoadPageStateFromPersistenceMedium()
{
    if (_viewStateInSession)
    {
        object objViewState = null;
        objViewState = Context.Session[key_viewState];
        return objViewState;
    }
    else
    {
        return base.LoadPageStateFromPersistenceMedium();
    }
}

/// <summary>
/// Na hora de Salvar o ViewState em variável de Sessão
/// </summary>
/// <param name="state"></param>
protected override void SavePageStateToPersistenceMedium(object state)
{
    if (_viewStateInSession)
        Context.Session[key_viewState] = state;
    else
        base.SavePageStateToPersistenceMedium(state);
}
#endregion

And to compress the HTML source code, we must add the following: 

#region [ CompressHtml ]
private bool _compressHtml = true;
public bool CompressHtml
{
    get
    {
        return _compressHtml;
    }
    set
    {
        _compressHtml = value;
    }
}
#endregion

#region [ Render ]
protected override void Render(HtmlTextWriter output)
{
    StringWriter outputWriter = new StringWriter();

    HtmlTextWriter normalOutput = new HtmlTextWriter(outputWriter);
    base.Render(normalOutput);
    normalOutput.Close();

    Session["_RENDER_"] = outputWriter.ToString();

    output.Write(RemoveSpaces(outputWriter.ToString()));
}

private string RemoveSpaces(string origin)
{
    if (!_compressHtml)
        return origin;

    int tam = origin.IndexOf("</head>");

    if (tam != -1)
    {
        tam += 7;
        string header = origin.Substring(0, tam);

        origin = origin.Replace(header, "");

        origin = origin.Replace("//<![CDATA[", " ");
        origin = origin.Replace("//]]>", " ");

        origin = Regex.Replace(origin, "\r\n", " ");
        origin = Regex.Replace(origin, "> <", "><");
        origin = Regex.Replace(origin, "javascript\"><!--", "javascript\">");
        origin = Regex.Replace(origin, "// --></script>\">", "</script>");
        return header + "\r\n" + Regex.Replace(origin, "\\s+", " ");
    }
    return origin;
}

#endregion

This indicates that a class is created that inherits the type Page (System.Web.Page) so that all pages inherit this system. So there is no need to add this code in all sources. 

Conclusion 

To make Web applications have acceptable performance there are several possible "bottlenecks" to be avoided. But there is usually a possible solution to your problem if you can find it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here