Simplify ASP.NET AJAX client-side initialization
AJAX, ASP.NET, UI By Dave Ward on August 1st, 2007I recently found myself needing to perform a bit of client-side page initialization, both on initial page load and when partial postbacks completed. I muddled through by using window.OnLoad() for the initial page load and an EndRequest handler for partial postbacks. It worked, but I wasn’t very happy with the kludgey nature of it.
On an unrelated ASP.NET forums thread around the same time, Steve Marx pointed out to me that the AJAX framework automatically wires up any client-side function named pageLoad() as an Application.Load handler. It’s actually in the docs, if you dig for it:
To handle the load and unload events of the Application object, you do not have to explicitly bind a handler to the event. Instead, you can create functions that use the reserved names pageLoad and pageUnload.
I was eager to replace my window.OnLoad handler with that. Window.OnLoad really isn’t optimal since it waits until the page is fully loaded (including images), which will almost always delay your script for longer than necessary.
After substituting in pageLoad, I discovered that it still had another trick up its sleeve. Turns out, pageLoad also runs after partial postbacks complete. At this point, I was nearly overjoyed (hey, it’s the small things) as I completely deleted my EndRequest handler.
A single, simple pageLoad function was all I had needed all along!
Summary
- Any JavaScript function named pageLoad() will automatically wireup as an Application.Load handler (basically, the client-side version of Page_Load).
- You’re usually better off using this instead of window.OnLoad().
- Application.Load is also fired when partial postbacks complete.
Have you stumbled upon any handy ASP.NET AJAX tricks or techniques like this? Leave a comment. I’d love to hear about them.
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
Thanks a lot mate. Wonderful finding.
What about the following?
When ajax completes yes pageLoad() executes, but what about when it start partial postback? Is there another method that we can use like pageLoad()?
InitializeRequest and BeginRequest are client side events that you can handle. Take a look at this post for an example of how to use them.
Thank you! I searched for something like this on msdn and google, but I guess search has a long way to go as i couldn’t find any reference to it!
Great stuff!
Thank you very much! This was exactly what i needed to get my jquery to work with the update panel. I had no idea that could wire a jscript function in by name alone. One suggestion though. I found this via google groups and had a hard time finding the post that you referenced (i got lucky with search :). You should add a link to that message with a reference to this specific post. Thanks again. You are my hero for the week!
EXCELLENT! Thank you. So simple.
I was curious about why “pageLoad” just magically worked. Thanks for the explanation.
Incidentally, it also works in a master page but you have to be careful where you insert your script block. If it’s too early in the file then it will not work. I put it in right near the bottom, before the ” tag and it works fine.
When using pageLoad() with master pages, this is one option: http://encosia.com/2007/11/20/using-pageload-in-both-master-and-content-pages/
That way, you can control the order of execution and don’t have to worry about placement issues.