The scenario is:

1. You’ve got an update on a page, and you have some jQuery executing a document.ready event.
2. The event isn’t firing when your update panel posts back asynchronously.

The solution is quite easy, take your original document.ready script and wrap it in a function and place it outside of your update panel (linked js file is fine):
function BindLoadEvents() { $(document).ready(function() { alert(‘fired!’); }); }

The bold text was the body of your original function.

Now, inside your update panel (preferably at the top):
<ContentTemplate>
<script type=”text/javascript”> if(typeof BindLoadEvents == ‘function’) Sys.Application.add_load(BindLoadEvents); </script>

Now, your script will execute when ajax has finished loading the page, including when new postbacks are made within your update panel!

Note that I have noticed slight flickering on the first request, to get around this, you can inline a call to BindLoadEvents(); within your <script> which sits outside of the update panel (below the BindLoadEvents function def), this will cause it to execute twice though, but the first time will be executed as the page renders, vs waiting for the page to finish loading, hence removing the slight delay.).  Let me know if there’s a nicer way to fix this glitch.

 

Advertisement

About cosier

Matthew Cosier is the Chief Technology Officer at Hazaa. We are a group of experienced consultants based in Melbourne, Australia who solve business problems using Microsoft Technology. For more information, please visit http://www.hazaa.com.au

One Response »

  1. [...] jQuery document ready not firing with ajax update panels [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s