Document.Ready() is not working after PostBack

前端 未结 8 1844
天命终不由人
天命终不由人 2020-12-02 18:39

I have a page that contains a user control within an update panel. $(document).ready(function() ) { is called and executes the code correctly when the page firs

相关标签:
8条回答
  • 2020-12-02 18:56

    Most of the times, this is happening because of the Updatepanle. Just put postback triggers to the button and it will solve this.

    0 讨论(0)
  • 2020-12-02 18:59

    I've run into this a while ago, as El Ronnoco said, it has to go with the DOM not being reloaded. However you can simply change $(document).ready(function() { to

    Sys.Application.add_load(function() {
    

    This will force it to run on every postback.

    You can use function pageLoad() as well, but you can only have one pageLoad function, whereas with Sys.Application.add_load, you can add as many handlers as you wish.

    0 讨论(0)
  • 2020-12-02 19:01

    Bestest way is

    <asp:UpdatePanel...
    <ContentTemplate
         <script type="text/javascript">
                        Sys.Application.add_load(LoadScript);
         </script>
     you hemla code gose here 
    </ContentTemplate>
        </asp:UpdatePanel>
    

    Javascript function

    <script type="text/javascript">
    
            function LoadScript() {
                $(document).ready(function() {
    
                       //you code gose here 
                                        });
             }
    </script>
    

    or

    Its under UpdatePanel than you need to register client script again using

    ScriptManager.RegisterClientScript
    

    or

    $(document).ready(function() {
        // bind your jQuery events here initially
    });
    
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    
    prm.add_endRequest(function() {
        // re-bind your jQuery events here
        loadscript();
    
    });
    
    
    $(document).ready(loadscript);
    
    function loadscript()
    {
      //yourcode 
    }
    
    0 讨论(0)
  • 2020-12-02 19:04

    This will be a problem with partial postback. The DOM isn't reloaded and so the document ready function won't be hit again. You need to assign a partial postback handler in JavaScript like so...

    function doSomething() {
       //whatever you want to do on partial postback
    }
    
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(doSomething);
    

    The above call to add_endRequest should be placed in the JavaScript which is executed when the page first loads.

    0 讨论(0)
  • 2020-12-02 19:10

    Instead of $(document).ready you could use function pageLoad(){}.

    It's automatically called by the ScriptManager on a page, even on a postback.

    0 讨论(0)
  • 2020-12-02 19:13

    This code below works nice to solve this problem. As indicated in link posted before (http://encosia.com/document-ready-and-pageload-are-not-the-same/), when you have an asp.NET with updatePanels you shall use function pageLoad(). When you have only one page and in each postback it will be fully reloaded, the $(document).ready() is the right option.

    Example using pageLoad:

        function pageLoad() {
    
            $(".alteraSoVirgula").keyup(function () {
                code here
            })
        }
    
    0 讨论(0)
提交回复
热议问题