开发者

jQuery outside of a asp.net update panel

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 开发者_JS百科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.


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.


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()">
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜