OdeToCode IC Logo

Master Page Performance

Saturday, July 1, 2006

Q: I'm having a problem with performance in my ASP.NET master page. In the debugger I see the master page runs during each postback. How do we stop the master page from running its Load event each time?

A: The MasterPage class derives from UserControl, which gives a hint as to why the master behaves this way. The master page becomes a control inside the page, and like every control in ASP.NET the master recreates itself on every postback (see Master Pages: Tips, Tricks, and Traps).

You might be able to improve the situation using some common ASP.NET performance tips:

  • Take areas of the master page that contain static content and place them into user controls. Place the user controls on the master page, and use output caching to avoid executing the code inside.
  • Check Page.IsPostBack during the master's Page_Load event handler and avoid performing unnecessary work. Use Atlas to refresh small sections of the page.
  • Evaluate the use of ViewState. If you can let controls restore themselves from ViewState you can avoid re-querying the database. The tradeoff is increased page size, so measure carefully.