Using pageLoad in both Master and Content pages
AJAX, ASP.NET, JavaScript By Dave Ward on November 20th, 2007Reader Kevin posed a good question in response to my last post, the answer to which is useful enough to warrant a separate post:
I have a situation where I call a page load handler from a script associated with a master page, then on a specific page based on that master page I need to call a second page load handler. I use the pageLoad() shortcut notation in the master page script. I can’t get either pageLoad() (expected since the composite page would have two pageLoad’s) or $addHandler(document, “load”, mySpecificPageLoad()) to work. I had to resort to my old “addLoadListener” function to add the second page load event handler. Is this as expected?
As he mentions, there’s only room in the page’s DOM for one pageLoad shortcut function, preventing both a master and content page from utilizing the shortcut simultaneously.
To work around this limitation, simply add this to your master’s pageLoad:
<script type="text/javascript"> function pageLoad() { // Master pageLoad() code. // If function contentPageLoad exists, execute it. if(typeof contentPageLoad == 'function') contentPageLoad(); }
Now, contentPageLoad is executed on any content page you wish to include it on, just as if it were pageLoad. If you don’t include it on a particular content page, the conditional will prevent its execution (and subsequent JavaScript error).
You could use Sys.Application.add_Load to wire up a custom pageLoad on every single content page, but I think the DRY-ness of this approach is preferable. The above method also allows you to retain control of the order of execution, through the placement of the contentPageLoad call in pageLoad.
Possibly related posts
What do you think? Your comments are welcomed.
I appreciate all of your comments, questions, and other feedback, but please try to stay on topic. If you have a question unrelated to this post, I recommend posting on the ASP.NET forums or Stack Overflow instead.
If you're replying to an existing comment, please use the threading feature. To do this, click the "Reply to this comment" link underneath the comment you're replying to.

Your comments
Hi Dave,
Good post. I think this is a sad limitation of pageLoad that it only works once. Now you solved it for the master-content scenario.
But the same problem happens for user controls in a page. If you want to define the pageLoad in several user controls in the same page this scenario would not solve that. I’m thinking of creating specific pageLoad methods for each control (e.g.: pageLoad_UserControlID) and then also rendering a setTimeout but I don’t like it actually…
Cya
Keep in mind that pageLoad() is just a shortcut function. You can wire up as many of them as you want, to arbitrarily named functions, with Sys.Application.add_init().
What a fantastic post! So simple and elegant.
I was scratching my head on this very issue. Glad your site popped high in the Google search results. http://www.google.com/search?q=Using+pageLoad+in+both+Master+and+Content&hl=en
superb article. you’re saving my time!
thanks for that