Click here to Skip to main content
Click here to Skip to main content

Performance of ASP.NET Forms Page

, 24 Jul 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Bruno B. Stoll
Software Developer (Senior) http://www.bstoll.com.br
Brazil Brazil
No Biography provided
Group type: Collaborative Group (No members)



Comments and Discussions

 
GeneralMy vote of 3 PinmemberEd Nutting24-Jul-12 8:19 
QuestionPerformance Improvment PinmemberKarthikeyan Ganesan24-Jul-12 6:18 
Generalsome questions Pinmembergiammin24-Jul-12 5:54 
why don't you use iis compression?
Why don't you use directly session instead of using viewstate and then save it to session?
GeneralRe: some questions PingroupBruno B. Stoll24-Jul-12 6:49 
GeneralRe: some questions Pinmembergiammin24-Jul-12 7:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141030.1 | Last Updated 24 Jul 2012
Article Copyright 2012 by Bruno B. Stoll
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid