I have a dynamically created block of JavaScript which executes on page load. The JS is inside a UpdatePanel.
When the UpdatePanel is updated, I need to update my dynamically created JavaScript. This all works fine as when I view the generated source of the page, I can see the updated JS.
My problem is that after the page is updated, I need to re-execute the JavaScript function manually (as this is normally done automatically on page load).
I've tried a number of methods:
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoaded);
function pageLoaded(sender, args) {
var updatedPanels = args.get_panelsUpdated();
// check if Main Panel was updated
for (idx = 0; idx < updatedPanels.length; idx++) {
if (updatedPanels[idx].id == "PageContent_TabContent_UpdatePanel1") {
drawVisualization();
break;
}
}
}
Or:
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(handleEndRequest);
function handleEndRequest(sender, args) {
drawVisualization();
}
But both of the above methods just execute the old version of the JavaScript, which according to the generated source code, has changed.
Is there something I'm missing, or maybe another method I could try?
UPDATE:
Using the comment @Mehdi Maujood, I've put together a working solution:
function handleEndRequest(sender, args)
{
var updatedPanels = args.get_panelsUpdated();
for (idx = 0; idx < updatedPanels.length; idx++) {
if (updatedPanels[idx].id == "PageContent_TabContent_UpdatePanel1") {
var scripts = updatedPanels[idx].getElementsByTagName('script');
for(var i=0;i<scripts.length;i++) {
eval(scripts[i].innerHTML);
}
drawVisualization();
break;
}
}
}
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(handleEndRequest);
This combines answers from How can I recall a javascript function after updatepanel update as well as @shatl's comment on Rebinding events in jQuery after Ajax update (updatepanel).