Using pageLoad in both Master and Content pages
AJAX, ASP.NET, JavaScript By Dave Ward; Updated October 10, 2008Reader 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.
Similar posts
What do you think?
I appreciate all of your comments, 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 another comment, use the threading feature by clicking "Reply to this comment" before submitting your own.



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().
Excelent! This is exactly what I’m looking for. I was using pageLoad before only to found out later the the user don’t work if there are several of them in one page. Thanks dave!
Dave,
you mention in your other article that pageLoad() function runs on every partial or regular postback, while the Sys.Application.add_init() wires a function that only executes once. So if i want to have a control that needs to run logic that does need to execute similarly to pageLoad() then how would this work? I might be missing a point here.
check out Sys.Application.add_load().
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
you really saved my time
Thanks For Info . :)