2

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 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!
0

Embed these lines on your javascript tag

function foo() { Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler); } function endRequestHandler(sender, args) { // Do your stuff alert('Update Panel routine is now complete'); } 

Then, put this on you body tag

<body onload="foo()"> 

1 Comment

Be careful when posting copy and paste boilerplate/verbatim answers to multiple questions, these tend to be flagged as "spammy" by the community. If you're doing this then it usually means the questions are duplicates so flag them as such instead.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.