在 asp.net 中触发 Updatepanel 后 Javascript 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24202349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript not work after firing Updatepanel in asp.net
提问by Saman Sadeghpour
This is my website -> www.superim.ir Its template base is bootstrap and in nav menu I used below code for some effects!
这是我的网站 -> www.superim.ir 它的模板基础是引导程序,在导航菜单中我使用了下面的代码来实现一些效果!
$('.productbar .dropdown').on('show.bs.dropdown', function (e) {
$(this).find('.dropdown-menu').first().stop(true, true).fadeIn(300);
$(this).find('.dropdown-menu').first().stop(true, true).animate({ top: "45px" }, 300);
});
$('.productbar .dropdown').on('hide.bs.dropdown', function (e) {
$(this).find('.dropdown-menu').first().stop(true, true).fadeOut(300);
$(this).find('.dropdown-menu').first().stop(true, true).animate({ top: "55px" }, 300);
$(this).find('.sub-menu').hide();
$(this).find('.left-caret').addClass("right-caret").removeClass("left-caret");
});
After firing add to cart button, updatepanel will fire and after this the menu effect does'nt work!
触发添加到购物车按钮后,更新面板将触发,此后菜单效果不起作用!
What is the solution?
解决办法是什么?
回答by SHEKHAR SHETE
This occurs due to the Partial Postback
using UpdatePanel
. The Events
that you subscribe for the controls are rendered partially hence the events looses. To overcome this situation you need to rebind the control events.
这是由于Partial Postback
使用UpdatePanel
. 在Events
您订阅的控件呈现局部因此事件失去。要克服这种情况,您需要重新绑定控制事件。
This is a common problem caused by mixing the conventional ASP.Net Ajax and jQuery events. When you do the partial postback, the DOM is recreated and the jQuery events are lost.
这是混合常规 ASP.Net Ajax 和 jQuery 事件导致的常见问题。当您执行部分回发时,会重新创建 DOM,并且会丢失 jQuery 事件。
Example:
例子:
<script type="text/javscript">
// bind the events (jQuery way)
$(document).ready(function() {
bindEvents();
});
// attach the event binding function to every partial update
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function(evt, args) {
bindEvents();
});
<script/>
Read More about PageRequest Manager on MSDN
在 MSDN 上阅读有关 PageRequest Manager 的更多信息
Here bindEvents()
method contains all the script that you need to reload again after Partial Page Postback.
HerebindEvents()
方法包含在部分页面回发后需要重新加载的所有脚本。
Hope this helps you!
希望这对你有帮助!
回答by Kamran
I faced the same problem. I got help from this link. Execute JavaScript when an UpdatePanel is updated
我遇到了同样的问题。我从这个链接得到了帮助。更新 UpdatePanel 时执行 JavaScript
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(
upPanel,
this.GetType(),
"MyAction",
"doMyAction();",
true);
}
- The first parameter is the UpdatePanel
- The second parameter is a type parameter
- The third parameter, “MyAction”, is a key to identify the script.
- script itself.
- The final parameter is a flag to indicate whether the ScriptManager should wrap your code in script tags.
- 第一个参数是 UpdatePanel
- 第二个参数是类型参数
- 第三个参数“MyAction”是识别脚本的关键。
- 脚本本身。
- 最后一个参数是一个标志,用于指示 ScriptManager 是否应该将您的代码包装在脚本标签中。