How can I run some javascript after an update panel refreshes?

匿名 (未验证) 提交于 2019-12-03 02:16:02

问题:

I have a pageLoad function which sets some css on an .ascx control that I cannot change. On page load everything is fine, but when an update panel updates the control, my css is no longer applied. How can I rerun my function after the page updates?

 $(function() {         $("textarea").attr("cols", "30");         $("input.tbMarker").css({ "width": "100px" }).attr("cols","25");     });

This obviously only runs on the initial page load. How can I run it after an update?

回答1:

Adding an add_pageLoaded handler can also work.

Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(PageLoadedHandler);

Note: the handler will fire for any callback, but you can use sender._postBackSettings.panelID to filter when you want your function called.

More samples:



回答2:

Most simple way is to use MSAjax pageLoad Event in your javascript code :

I have used it many times, it works like charm. Most important thing is don't confuse it with document.ready function (which will be executed only once after the page Document Object Model (DOM) is ready for JavaScript code to execute),yet pageLoad Event will get executed every time the update panel refreshes.

Source: Running script after Update panel AJAX asp.net



回答3:

During your postback for the update panel, in the server code, use ClientScriptManager to add some new script to the page, something like this:

ClientScriptManager.RegisterStartupScript(        typeof(page1),         "CssFix",         "javascriptFunctionName()",          true);

Encapsulate your javascript in a named function that matches the third argument there, and it should execute when the postback returns.



回答4:

Add the code in the same form you placed your Script Manager.

Code Behind:

protected void Page_Load(object sender, EventArgs e) {     if (ScriptManager1.IsInAsyncPostBack)     {         StringBuilder sb = new StringBuilder();         sb.Append("");         ScriptManager.RegisterStartupScript(this, GetType(), "script", sb.ToString(), false);     } }


回答5:

You can also bind to an event in client side code (JavaScript) every time an UpdatePanel has finished like this:

        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function(){myFunction();});

So in this case myFunction(); will be called every time an UpdatePanel postback has occurred. If you execute this code when the page is loaded the function will be called on the correct time.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!