[英]Why does my function run multiple times after UpdatePanel is loaded
So I want to run some javaScript function after my updatepanel is updated, so I have: 所以我想在我的updatepanel更新后运行一些javaScript函数,所以我有:
function pageLoad() {
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_pageLoaded(panelLoaded);
}
function panelLoaded(sender, args) {
alert("foobar");
}
With the above code, if I update the panel one time, "foobar" will be alerted one time; 使用上面的代码,如果我一次更新面板,“ foobar”将被警告一次; If I update the panel the second time, "foobar" will pop up twice;
如果我第二次更新面板,“ foobar”将弹出两次; the third time I trigger the panel to update, "foobar" popped up three times... 4th time pop 4 times so on and so forth....
第三次触发面板更新时,“ foobar”弹出了3次...第4次弹出了4次,依此类推。
What caused this?? 是什么原因造成的??
Thanks~~~ 谢谢~~~
This is because pageLoad is executed during updatepanel postbacks as well. 这是因为pageLoad也在updatepanel回发期间执行。 There is an easy solution:
有一个简单的解决方案:
function pageLoad(sender, eventArgs) {
// If this is being executed by ajax
if (eventArgs) {
// And this is only a partial load
if (eventArgs.get_isPartialLoad()) {
// Don't perform any further processing
return;
}
}
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_pageLoaded(panelLoaded);
}
Thanks all, problem seem to be having too many prm instances as Sam mentioned. 谢谢大家,问题似乎像Sam提到的那样有太多的prm实例。 I added Sys.WebForms.PageRequestManager.getInstance().remove_pageLoaded(panelLoaded);
我添加了Sys.WebForms.PageRequestManager.getInstance()。remove_pageLoaded(panelLoaded); after the alert() and everything is good.
在alert()之后,一切都很好。
This is what is happening. 这就是正在发生的事情。
pageLoad
basically functions as a combination of Application.Init and PageRequestManager.EndRequest . pageLoad
基本上是Application.Init和PageRequestManager.EndRequest的组合。 That means it works on Application Initialization ( approximately DOM Ready ) and on each Partial PostBack
这意味着它可以在应用程序初始化(大约支持DOM就绪)和每个
Partial PostBack
So pageLoad works on all PostBacks; 因此pageLoad可在所有PostBacks上使用; full and partial.
全部和部分。
Now you are binding up pageLoaded event again on the ScriptManager's instance on every partial postback again and again causing this behaviour. 现在,您在每次部分回发时都在ScriptManager的实例上再次绑定pageLoaded事件,一次又一次导致此行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.