I 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.