jQuery $(document).ready and UpdatePanels?

后端 未结 19 1524
庸人自扰
庸人自扰 2020-11-22 01:32

I\'m using jQuery to wire up some mouseover effects on elements that are inside an UpdatePanel. The events are bound in $(document).ready . For example:

相关标签:
19条回答
  • 2020-11-22 01:41

    FWIW, I experienced a similar issue w/mootools. Re-attaching my events was the correct move, but needed to be done at the end of the request..eg

    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(function() {... 
    

    Just something to keep in mind if beginRequest causes you to get null reference JS exceptions.

    Cheers

    0 讨论(0)
  • 2020-11-22 01:45

    You could also try:

    <asp:UpdatePanel runat="server" ID="myUpdatePanel">
        <ContentTemplate>
    
            <script type="text/javascript" language="javascript">
            function pageLoad() {
               $('div._Foo').bind("mouseover", function(e) {
                   // Do something exciting
               });
            }
            </script>
    
        </ContentTemplate>
    </asp:UpdatePanel>
    

    ,since pageLoad() is an ASP.NET ajax event which is executed each time the page is loaded at client side.

    0 讨论(0)
  • 2020-11-22 01:46

    My answer is based on all the expert comments above, but below is the following code that anyone can use to make sure on each postback and on each asynchronous postback the JavaScript code will still be executed.

    In my case, I had a user control within a page. Just paste the below code in your user control.

    <script type="text/javascript"> 
            var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_endRequest(EndRequestHandler);
        function EndRequestHandler(sender, args) {
            if (args.get_error() == undefined) {
                UPDATEPANELFUNCTION();
            }                   
        }
    
        function UPDATEPANELFUNCTION() {
            jQuery(document).ready(function ($) {
                /* Insert all your jQuery events and function calls */
            });
        }
    
        UPDATEPANELFUNCTION(); 
    
    </script>
    
    0 讨论(0)
  • 2020-11-22 01:47

    This worked for me:

    $(document).ready(function() {
    
        // Do something exciting
    
        var prm = Sys.WebForms.PageRequestManager.getInstance();
    
        prm.add_endRequest(function() {
            // re-bind your jQuery events here
        });
    
    });
    
    0 讨论(0)
  • 2020-11-22 01:49

    I had a similar problem and found the way that worked best was to rely on Event Bubbling and event delegation to handle it. The nice thing about event delegation is that once setup, you don't have to rebind events after an AJAX update.

    What I do in my code is setup a delegate on the parent element of the update panel. This parent element is not replaced on an update and therefore the event binding is unaffected.

    There are a number of good articles and plugins to handle event delegation in jQuery and the feature will likely be baked into the 1.3 release. The article/plugin I use for reference is:

    http://www.danwebb.net/2008/2/8/event-delegation-made-easy-in-jquery

    Once you understand what it happening, I think you'll find this a much more elegant solution that is more reliable than remembering to re-bind events after every update. This also has the added benefit of giving you one event to unbind when the page is unloaded.

    0 讨论(0)
  • 2020-11-22 01:49
    pageLoad = function () {
        $('#div').unbind();
        //jquery here
    }
    

    The pageLoad function is perfect for this case since it runs on the initial page load and every updatepanel async postback. I just had to add the unbind method to make the jquery work on updatepanel postbacks.

    http://encosia.com/document-ready-and-pageload-are-not-the-same/

    0 讨论(0)
提交回复
热议问题