jQuery $(document).ready and UpdatePanels?

后端 未结 19 1525
庸人自扰
庸人自扰 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:50

    Upgrade to jQuery 1.3 and use:

    $(function() {
    
        $('div._Foo').live("mouseover", function(e) {
            // Do something exciting
        });
    
    });
    

    Note: live works with most events, but not all. There is a complete list in the documentation.

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

    Update Panel always replaces your Jquery with its inbuilt Scriptmanager's scripts after every load. Its better if you use pageRequestManager's instance methods like this...

    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(onEndRequest)
        function onEndRequest(sender, args) {
           // your jquery code here
          });
    

    it will work fine ...

    0 讨论(0)
  • 2020-11-22 01:51
    <script type="text/javascript">
    
            function BindEvents() {
                $(document).ready(function() {
                    $(".tr-base").mouseover(function() {
                        $(this).toggleClass("trHover");
                    }).mouseout(function() {
                        $(this).removeClass("trHover");
                    });
             }
    </script>
    

    The area which is going to be updated.

    <asp:UpdatePanel...
    <ContentTemplate
         <script type="text/javascript">
                        Sys.Application.add_load(BindEvents);
         </script>
     *// Staff*
    </ContentTemplate>
        </asp:UpdatePanel>
    
    0 讨论(0)
  • 2020-11-22 01:52

    When $(document).ready(function (){...}) not work after page post back then use JavaScript function pageLoad in Asp.page as follow:

    <script type="text/javascript" language="javascript">
    function pageLoad() {
    // Initialization code here, meant to run once. 
    }
    </script>
    
    0 讨论(0)
  • 2020-11-22 01:52

    Use below script and change the body of the script accordingly.

           <script>
            //Re-Create for on page postbacks
            var prm = Sys.WebForms.PageRequestManager.getInstance();
            prm.add_endRequest(function () {
               //your codes here!
            });
        </script>
    
    0 讨论(0)
  • function pageLoad() is very dangerous to use in this situation. You could have events become wired multiple times. I would also stay away from .live() as it attaches to the document element and has to traverse the entire page (slow and crappy).

    The best solution I have seen so far is to use jQuery .delegate() function on a wrapper outside the update panel and make use of bubbling. Other then that, you could always wire up the handlers using Microsoft's Ajax library which was designed to work with UpdatePanels.

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