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.

Rick Strahl Monday, July 3, 2006

I've been asking myself some of those questions recently as well, although I've found that MasterPage performance is pretty good for all that it provides. About the worst 'overhead' I would say it has is that there's a fair amount of fix up for control names and header injection etc. that adds a bit to the size of the page. But performance wise I don't see a big noticable difference.

I ran a few stress test sessions comparing a straight page that basically assembled output using user controls and the same page that used a master page and there was no discernable difference in page performance (in fact it wobbled back and forth with about 2% margin of error both ways).

Of course if you add lots of code to a Master (not the best of ideas) then that code has to run, but that's true of anything.

As to using UserControls I agree on that call. Howver, I've run into a number of issues if you have inherited MasterPages where multiple master pages are using the same master base class and controls added to that would cause very weird naming conflict at least in stock projects. Something to watch out for...

I for one was pleasantly surprised that there wasn't a perf penalty for paster pages beyond what you would expect for a control.
Comments are closed.