简体   繁体   English

回发后 Document.Ready() 不起作用

[英]Document.Ready() is not working after PostBack

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 firsts loads but if the user clicks a button (within the user control), the document.ready() doesn't get called ( document.load , onload also don't work) $(document).ready(function() ) {在页面首次加载时被调用并正确执行代码,但如果用户单击按钮(在用户控件内),则不会调用document.ready() ( document.loadonload也不起作用)

I have researched this on the net and found similar problems but nothing that can explain why this isn't working.我在网上对此进行了研究并发现了类似的问题,但没有任何内容可以解释为什么这不起作用。 What other causes can there be for document.ready not working? document.ready不工作还有哪些其他原因?

This will be a problem with partial postback .这将是部分回发的问题。 The DOM isn't reloaded and so the document ready function won't be hit again. DOM 不会重新加载,因此文档就绪函数不会再次被触发。 You need to assign a partial postback handler in JavaScript like so...您需要像这样在 JavaScript 中分配一个部分回发处理程序......

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.上面对add_endRequest调用应该放在页面第一次加载时执行的 JavaScript 中。

Instead of $(document).ready you could use function pageLoad(){} .您可以使用function pageLoad(){}代替$(document).ready

It's automatically called by the ScriptManager on a page, even on a postback.它由ScriptManager在页面上自动调用,即使在回发时也是如此。

I've run into this a while ago, as El Ronnoco said, it has to go with the DOM not being reloaded.正如 El Ronnoco 所说,我不久前遇到过这个问题,它必须与 DOM 不重新加载。 However you can simply change $(document).ready(function() { to但是,您可以简单地将$(document).ready(function() {更改为

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.您也可以使用 pageLoad() 函数,但您只能有一个 pageLoad 函数,而使用 Sys.Application.add_load,您可以添加任意数量的处理程序。

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 Javascript 函数

<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它在 UpdatePanel 下比您需要使用再次注册客户端脚本

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 
}

This is an example that worked for me in the past:这是一个过去对我有用的例子:

<script>
function MyFunction(){ 
    $("#id").text("TESTING");
}
//Calling MyFunction when document is ready (Page loaded first time)
$(document).ready(MyFunction); 

//Calling MyFunction when the page is doing postback (asp.net)
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(MyFunction);
</script>

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().如之前发布的链接所示( http://encosia.com/document-ready-and-pageload-are-not-the-same/ ),当你有一个带有 updatePanels 的 asp.NET 时,你应该使用函数 pageLoad()。 When you have only one page and in each postback it will be fully reloaded, the $(document).ready() is the right option.当您只有一页并且在每次回发时它将完全重新加载, $(document).ready() 是正确的选择。

Example using pageLoad:使用 pageLoad 的示例:

    function pageLoad() {

        $(".alteraSoVirgula").keyup(function () {
            code here
        })
    }

I was also facing the same problem but i found the jQuery $(document).ready event handler works when page loads, but after ASP.Net AJAX UpdatePanel Partial PostBack it does not get called.我也面临同样的问题,但我发现 jQuery $(document).ready 事件处理程序在页面加载时工作,但在 ASP.Net AJAX UpdatePanel Partial PostBack 之后它不会被调用。 so use Sys.Application.add_load(function(){});所以使用 Sys.Application.add_load(function(){}); instead of $(document).ready.而不是 $(document).ready。 This works perfectly for me :)这对我来说非常有效:)

<script>
Sys.Application.add_load(function() { //Your code }); </script>

$(document).ready(function () {
    PreRoles();
});

//On UpdatePanel Refresh
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm != null) {
    prm.add_endRequest(function (sender, e) {
        if (sender._postBackSettings.panelsToUpdate != null) {
            PreRoles();
        }
    });
};

function PreRoles() {
    // Add codes that should be called on postback
}

Most of the times, this is happening because of the Updatepanle.大多数情况下,这是由于 Updatepanle 而发生的。 Just put postback triggers to the button and it will solve this.只需将回发触发器放在按钮上即可解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM