How to troubleshoot slow Page Rendering in ASP.NET?

by rahul 11/20/2009 2:49:44 PM

Before I answer this question, let us have a quick refresher. Answer this question diligently… Is Page_Render in ASP.NET really an EVENT? A BIG ZERO if you have said YES!

The answer is No, It is not. Here is an excerpt from MSDN…

<snip>

This is not an event; instead, at this stage of processing, the Page object calls this method on each control. All ASP.NET Web server controls have a Render method that writes out the control's markup that is sent to the browser.

If you create a custom control, you typically override this method to output the control's markup. However, if your custom control incorporates only standard ASP.NET Web server controls and no custom markup, you do not need to override the Render method. For more information, see Developing Custom ASP.NET Server Controls.

A user control (an .ascx file) automatically incorporates rendering, so you do not need to explicitly render the control in code.

</snip>

See the following trace… you can see that Page is taking a long time to render. Your page has multiple controls, and you have to figure out which control is causing the issue. How will you do it? Oh, I forgot to say, you are on a Production server, and you don’t have the code for the controls that are present on your page.

image

Page level tracing can only show that you are taking a lot of time in Rendering the page. How can you smartly tell which control is causing the issue is the topic for this post.

I am going to use a dirty trick in the form of ControlAdapters. Read More

    Create a folder called App_Code inside your application
    Add a new class ControlAdapterTracing.cs
    Copy and past the following code…

public class ControlAdapterTracing : System.Web.UI.Adapters.ControlAdapter
{
    protected override void BeginRender(HtmlTextWriter writer)
    {
        this.Page.Trace.Write("Started Rendering of :" + this.Control.ID + " - " + this.Control.GetType().FullName);
        base.BeginRender(writer);
    }

    protected override void EndRender(HtmlTextWriter writer)
    {
        base.EndRender(writer);
        this.Page.Trace.Write("Rendering Complete for :" + this.Control.ID + " - " + this.Control.GetType().FullName);
    }
}

     Configure this adapter now by creating a new folder called App_Browsers directly inside your application
     Add a new file in App_Browsers folder called Tracing.browser
     Copy and past the following in Tracing.browser file…

  <browser refID="default">
    <controlAdapters>
      <adapter controlType="System.Web.UI.HtmlControls.HtmlForm" adapterType="ControlAdapterTracing" />
      <adapter controlType="System.Web.UI.WebControls.Button" adapterType="ControlAdapterTracing" />
      <adapter controlType="System.Web.UI.UserControl" adapterType="ControlAdapterTracing" />
    </controlAdapters>
  </browser>

     If you have 3rd party controls, or other controls like GridView, FormView, etc… you must add adapter entries for all the controlTypes as you can see above. Please note that it should be fully qualified…

<adapter controlType=”NameSpace.ControlName, DLLName, Version=x.x.x.x, Culture=neutral, PublicKeyToken=PublicKeyOfControl” adapaterType=”ControlAdapterTracing” />

     Once done, if you execute your page you should see something like the following now (compare it to the screenshot above)… 

image  

Awesome, I am now aware that the culprit here is my ASP.Control2_ascx and I can take appropriate actions going forward. <Where is the bugger who wrote this code?>

Hope this helps, Wave
Rahul


Quote of the day:
He who asks is a fool for five minutes, but he who does not ask remains a fool forever. - Chinese Proverb

Tags: , , ,

ASP.NET | Tips and Tricks | Troubleshooting | Performance

blog comments powered by Disqus

Who is Rahul?

Rahul

He works for Microsoft and likes Software & Gadgets!

He shares some interesting notes here while working with stuff that excites him.

Disclaimer

The opinions expressed herein are our own personal opinions and do not represent our employer's view in any way.
© Copyright 2014, Rahul Soni