Ajax 更新后在 jQuery 中重新绑定事件 (updatepanel) [英] Rebinding events in jQuery after Ajax update (updatepanel)

查看:42
本文介绍了Ajax 更新后在 jQuery 中重新绑定事件 (updatepanel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有几个 input 和 option 元素,每个(几乎)都有一个附加的事件,一旦它们发生变化,就会更新页面上的一些文本.我使用 jQuery,它真的很酷 :)

I have several input and option elements on my page, each (well almost) have an event attached to update some text on the page once they change. I use jQuery which is really really cool :)

我还使用 Microsoft 的 Ajax 框架,利用 UpdatePanel.我这样做的原因是某些元素是基于某些服务器端逻辑在页面上创建的.我真的不想解释为什么我使用 UpdatePanel - 即使它可以(它可以通过相当多的努力)被重写为仅使用 jQuery 我仍然想要那个 UpdatePanel.

I also use Microsofts Ajax framework, utilizing the UpdatePanel. The reason why I do that is that certain elements are created on the page based on some server-side logic. I don't really want to explain why I use the UpdatePanel - even if it could (it can with quite some effort) be rewritten to use only jQuery I still want that UpdatePanel.

您可能已经猜到了 - 一旦我在 UpdatePanel 上进行回发,jQuery 事件就会停止工作.我实际上在期待这一点,因为回发"并不是真正的新回发,所以我在 document.ready 中绑定事件的代码不会再次被触发.我还通过阅读 jQuery 帮助库中的内容证实了我的怀疑.

You probably guessed it - once I have a postback on the UpdatePanel the jQuery events stops working. I actually was expecting this, since the "postback" is not really a new postback so my code in document.ready that binds the events won't be fired again. I also confirmed my suspicion by reading up on it in the jQuery help libraries.

无论如何,在 UpdatePanel 完成更新 DOM.我最好需要一个不需要向页面添加更多 .js 文件(jQuery 插件)的解决方案,而是像能够捕获 UpdatePanel 的更新后"一样简单的解决方案,我可以在其中调用我的方法来重新绑定所有表单元素.

Anyway I'm left with the problem of rebinding my controls after the UpdatePanel is done updating the DOM. I preferably need a solution that does not require adding more .js files (jQuery plug-ins) to the page but something as simple as being able to catch the UpdatePanel's 'afterupdating' where I can just call my method to rebind all the form elements.

推荐答案

由于您使用的是 ASP.NET AJAX,您将可以访问每次都会调用的 pageLoad 事件处理程序页面回传,无论是完整的还是部分来自 UpdatePanel.你只需要把这个函数放到你的页面中,不需要连接.

Since you're using ASP.NET AJAX, you'll have access to a pageLoad event handler, that gets called each time the page posts back, be it full or partial from an UpdatePanel. You just need to put the function in to your page, no hooking up is required.

function pageLoad(sender, args)
{
   if (args.get_isPartialLoad())
   {
       //Specific code for partial postbacks can go in here.
   }
}

这篇关于Ajax 更新后在 jQuery 中重新绑定事件 (updatepanel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆