如何在更新面板回发后执行 JavaScript 回调?
我使用 jQuery 提示插件在用户将鼠标悬停在页面的某些元素上时显示帮助提示。
我需要在使用 css 选择器加载页面后注册插件事件。
问题是我正在使用 ASP.NET 更新面板,并且在第一次回发后,提示停止工作,因为更新面板替换了页面内容,但没有重新绑定 javascript 事件。
我需要一种在更新面板刷新其内容后执行 JavaScript 回调的方法,以便我可以重新绑定 JavaScript 事件以使提示再次工作。
有什么办法可以做到这一点吗?
I'm using a jQuery tip plugin to show help tips when the user hovers certain elements of the page.
I need to register the plugin events after the page is loaded using css selectors.
The problem is I'm using an ASP.NET Update Panel and after the first postback, the tips stop working because the update panel replaces the page content but doesn't rebind the javascript events.
I need a way to execute a javascript callback after the Update Panel refreshes its content, so I can rebind the javascript events to have the tips working again.
Is there any way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要将 jQuery 代码放入
$(document).ready()
中,而是将其放入pageLoad
中,该代码在每次回发(同步或异步)后执行。pageLoad
是 ASP.NET AJAX 中为此目的保留的函数名称。 另一方面,$(document).ready()
仅在 DOM 最初准备好/加载时执行一次。请参阅ASP.NET AJAX 客户端生命周期事件概述
Instead of putting your jQuery code inside of
$(document).ready()
, put it insidepageLoad
is executed after every postback, synchronous or asynchronous.pageLoad
is a reserved function name in ASP.NET AJAX that is for this purpose.$(document).ready()
on the other hand, is executed only once, when the DOM is initially ready/loaded.See this Overview of ASP.NET AJAX client lifecycle events
页面加载不起作用。 我用这个代替:
The pageLoad didn't work. I used this instead:
这可能不是最优雅的解决方案,但它仍然是一个解决方案:
像这样使用它:
当然,您需要在 webconfig 或页面中注册自定义控件才能像这样使用它。
编辑:另外,您看过 jquery.live 吗?
This is probably far from the most elegant solution, but its a solution nonetheless:
Use it like this:
Of course you'll need to register the custom control in youre webconfig or page to use it like this.
Edit: also, have you looked at jquery.live?
如果您想在 UpdatePanel 加载之前和之后执行特定操作,您可以重写
BeginPostbackRequest
和EndPostbackRequest
,如下所示:如果您只想处理以下 HTML,这非常方便:由更新面板提供。 有些操作需要更多资源,在
pageLoad
上处理整个 DOM 树就显得有些过分了。If you want to do specific operations before and after the UpdatePanel has loaded, you can override
BeginPostbackRequest
andEndPostbackRequest
like so:This is very handy if you want to process only HTML that was delivered by the update panel. Some operations require more resources and it would be overkill to process the whole DOM tree on
pageLoad
.使用 pageLoaded 事件并检查是否回调或回发:
Use pageLoaded event and check whether callback or postback: