Ajax 更新后重新绑定 jQuery 事件 (updatepanel)

发布于 2024-07-09 06:45:58 字数 802 浏览 4 评论 0原文

我的页面上有几个输入和选项元素,每个元素(几乎)都附加了一个事件,用于在页面上的某些文本发生更改时更新它们。 我使用 jQuery,这真的很酷:)

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

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

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

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 :)

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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

铁轨上的流浪者 2024-07-16 06:45:58

由于您使用的是 ASP.NET AJAX,因此您可以访问 pageLoad 事件处理程序,每次页面回发时都会调用该事件处理程序,无论是从 UpdatePanel 回发完整页面还是部分页面。 您只需将该功能放入您的页面即可,无需连接。

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

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.
   }
}
中性美 2024-07-16 06:45:58

或者您可以通过 on() 检查最新的 jQuery 实时功能方法。

Or you could check the latest jQuery's live functionality via the on() method.

瑾夏年华 2024-07-16 06:45:58
Sys.Application.add_load(initSomething);
function initSomething()
{
  // will execute on load plus on every UpdatePanel postback
}
Sys.Application.add_load(initSomething);
function initSomething()
{
  // will execute on load plus on every UpdatePanel postback
}
欲拥i 2024-07-16 06:45:58

从 jQuery 1.7 开始,推荐的方法是使用 jQuery 的 .on() 语法。

但请确保您在 document 对象上设置事件,而不是在 DOM 对象本身上。 例如,这将在 UpdatePanel 回发后中断

$(':input').on('change', function() {...});

...因为 ':inputs' 已被重写。 相反,请执行以下操作:

$(document).on('change', ':input', function() {...});

只要文档存在,任何输入(包括来自 UpdatePanel 刷新的输入)都将触发更改处理程序。

As of jQuery 1.7, the recommended way to do this is to use jQuery's .on() syntax.

Make sure, however, that you set up the event on the document object, not the DOM object itself. For example, this will break after the UpdatePanel postback:

$(':input').on('change', function() {...});

... because the ':inputs' have been rewritten. Do this instead:

$(document).on('change', ':input', function() {...});

As long as the document is around, any inputs (including those from UpdatePanel refreshes) will trigger the change handler.

狼亦尘 2024-07-16 06:45:58

使用以下代码

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

function pageLoaded(sender, args) {
    var updatedPanels = args.get_panelsUpdated();
    // check if Main Panel was updated 
    for (idx = 0; idx < updatedPanels.length; idx++) {
        if (updatedPanels[idx].id == "<%=upMain.ID %>") {
            rebindEventsForMainPanel();
            break;
        }
    }
}

Use following code

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

function pageLoaded(sender, args) {
    var updatedPanels = args.get_panelsUpdated();
    // check if Main Panel was updated 
    for (idx = 0; idx < updatedPanels.length; idx++) {
        if (updatedPanels[idx].id == "<%=upMain.ID %>") {
            rebindEventsForMainPanel();
            break;
        }
    }
}
箹锭⒈辈孓 2024-07-16 06:45:58

您可以使用 jQuery 和事件委托。 基本上将事件挂钩到容器而不是每个元素,并查询 event.target 并基于该目标运行脚本。

它有多种好处,可以减少代码噪音(无需重新绑定)。 这是
也更容易占用浏览器内存(DOM 中绑定的事件更少。)

简单示例此处

jQuery 插件,用于轻松事件委托。

PS 我 99% 确信代表团将在下一个版本中出现在 jQuery 核心中。

You could use jQuery and event delegation. Basically hook events to containers rather than every element and query the event.target and run script based on that.

It has multiple benefits in that you reduce the code noise (no need to rebind). It is
also easier on browser memory (less events bound in the DOM.)

Quick example here.

jQuery plugin for easy event delegation.

P.S I am 99% sure delegation will be in the jQuery core at the next release.

段念尘 2024-07-16 06:45:58

使用以下代码,您需要验证控件将使用datapicker:

    <script type="text/javascript" language="javascript">

         Sys.WebForms.PageRequestManager.getInstance().add_endRequest(addDataPicker); 
         function addDataPicker(sender, args)
         {
            var fchFacturacion = document.getElementById('<%= txtFechaFacturacion.ClientID %>');
            if (fchFacturacion != null) {
               $(fchFacturacion).datepicker({ onSelect: function () { }, changeMonth: true, changeYear: true, showOn: 'button', buttonImage: '../Imagenes/calendar.gif', buttonImageOnly: true});}
         } 

    </script>

     <asp:UpdatePanel ID="upEjem" runat="server" UpdateMode="Conditional">
       <ContentTemplate>
              <div id="div1" runat="server" visible="false">
                  <input type="text" id="txtFechaFacturacion" 
                      name="txtFechaFacturacion" visible="true"
                      readonly="readonly" runat="server" />
              </div>
       </ContentTemplate>
     </asp:UpdatePanel>

Use the following code, You need to validate the control will use the datapicker:

    <script type="text/javascript" language="javascript">

         Sys.WebForms.PageRequestManager.getInstance().add_endRequest(addDataPicker); 
         function addDataPicker(sender, args)
         {
            var fchFacturacion = document.getElementById('<%= txtFechaFacturacion.ClientID %>');
            if (fchFacturacion != null) {
               $(fchFacturacion).datepicker({ onSelect: function () { }, changeMonth: true, changeYear: true, showOn: 'button', buttonImage: '../Imagenes/calendar.gif', buttonImageOnly: true});}
         } 

    </script>

     <asp:UpdatePanel ID="upEjem" runat="server" UpdateMode="Conditional">
       <ContentTemplate>
              <div id="div1" runat="server" visible="false">
                  <input type="text" id="txtFechaFacturacion" 
                      name="txtFechaFacturacion" visible="true"
                      readonly="readonly" runat="server" />
              </div>
       </ContentTemplate>
     </asp:UpdatePanel>
娜些时光,永不杰束 2024-07-16 06:45:58
         <script type="text/javascript">
             function pageLoad() {

                 if (Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) {


       }

            </script>

        </ContentTemplate>
    </asp:UpdatePanel>

在“if”中,您可以放入每次 updatepanel 执行 AsyncPostBack 时需要执行的代码。

         <script type="text/javascript">
             function pageLoad() {

                 if (Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) {


       }

            </script>

        </ContentTemplate>
    </asp:UpdatePanel>

into of the "if" you can put the code that you need execute every time that the updatepanel does AsyncPostBack.

留一抹残留的笑 2024-07-16 06:45:58

使用 jQuery 的新“live”方法绑定您的事件。 它将事件绑定到所有当前元素以及所有未来元素。 查清! :)

Bind your events using jQuery's new 'live' method. It will bind events to all your present elements and all future ones too. Cha ching! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文