Click here to Skip to main content
13,595,773 members
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 24 Jul 2012
Licenced CPOL

Performance of ASP.NET Forms Page

, 24 Jul 2012
Rate this:
Please Sign up or sign in to vote.
Improving page performance.


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.


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
        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;
        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;

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

#region [ CompressHtml ]
private bool _compressHtml = true;
public bool CompressHtml
        return _compressHtml;
        _compressHtml = value;

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

    HtmlTextWriter normalOutput = new HtmlTextWriter(outputWriter);

    Session["_RENDER_"] = 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;


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. 


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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Bruno B. Stoll
Software Developer (Senior)
Brazil Brazil
This member doesn't quite have enough reputation to be able to display their biography and homepage.
Group type: Collaborative Group (No members)

You may also be interested in...

Comments and Discussions

GeneralMy vote of 3 Pin
Ed Nutting24-Jul-12 8:19
memberEd Nutting24-Jul-12 8:19 
QuestionPerformance Improvment Pin
Karthikeyan Ganesan24-Jul-12 6:18
memberKarthikeyan Ganesan24-Jul-12 6:18 
1. I don't think it is valid suggesion,
Reason: Once we add some object in to Session, regardless wheter it is used or not a the Session Object will get from the storage medium (Memory/SQL/Session) for every page request, also the updated version get persisted in the undlaying Session Storage Area. Which will create performance and Scalability Issue.
2. Use ISS Compression
3. Try to Store the Client if its static. (JavaScript Object)
Senior Product Designer,
iSOFT, Chennai

Generalsome questions Pin
giammin24-Jul-12 5:54
membergiammin24-Jul-12 5:54 
GeneralRe: some questions Pin
Bruno B. Stoll24-Jul-12 6:49
groupBruno B. Stoll24-Jul-12 6:49 
GeneralRe: some questions Pin
giammin24-Jul-12 7:41
membergiammin24-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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03-2016 | 2.8.180621.3 | Last Updated 24 Jul 2012
Article Copyright 2012 by Bruno B. Stoll
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid