Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET

Performance of ASP.NET Forms Page

Rate me:
Please Sign up or sign in to vote.
3.77/5 (3 votes)
24 Jul 2012CPOL1 min read 14.1K   7   5
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: 

C++
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: 

C++
#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)


Written By
Software Developer (Senior) http://www.bstoll.com.br
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
This is a Collaborative Group (No members)


Comments and Discussions

 
GeneralMy vote of 3 Pin
Ed Nutting24-Jul-12 8:19
Ed Nutting24-Jul-12 8:19 
A nice idea and possibly the solution for your specific case but I agree with others, there are much better alternatives and this approach should not be recommended.
Ed
QuestionPerformance Improvment Pin
Karthikeyan Ganesan24-Jul-12 6:18
Karthikeyan Ganesan24-Jul-12 6:18 
Generalsome questions Pin
giammin24-Jul-12 5:54
giammin24-Jul-12 5:54 
GeneralRe: some questions Pin
Bruno B. Stoll24-Jul-12 6:49
Bruno B. Stoll24-Jul-12 6:49 
GeneralRe: some questions Pin
giammin24-Jul-12 7:41
giammin24-Jul-12 7:41 

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

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