views:

26

answers:

1

I have an asp.net page that is using and update panel and am including some javascript via a {script src=abc.js} tag. I have tried loading my javascript using both $(document).ready and $(window).load. Both fire on the initial load but fail to fire on an update panel postback.

Is there anything I can wire up client side that would fire regardless of the update panel? I know I can inject script server side but I ideally want to do this all client side and wire up what ever is needed the first time around.

+2  A: 

If you want to register some code to always run even on an UpdatePanel postback, you will need to register your function to be called when an update occurs. You can register a javascript function to run by doing the following:

// Javascript
function InitializeScripts()
{
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_pageLoaded(YourJavascriptFunctionYouWantToRun);
}

The PageRequestManager will call your javascript function when the UpdatePanel refreshes.

I have used this in the past to cause a trigger when an UpdatePanel fired a refresh due to a timer. When the refresh completed, the scripts would automatically run to update other controls based on the data that was displayed.

See this MSDN article for more information.

Kelsey
I added a bit of type checking in the event that the page does not contain an update panel.if (typeof (Sys) != "undefined") { var manager = Sys.WebForms.PageRequestManager.getInstance(); manager.add_pageLoaded(setup); } else { setup(); }});Works great!
Andrew Robinson