C# 回发后 Document.Ready() 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9586903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Document.Ready() is not working after PostBack
提问by Theomax
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, onloadalso don't work)
我有一个页面,其中包含更新面板中的用户控件。$(document).ready(function() ) {被称为正确执行代码时的页面加载首创,但如果用户点击一个按钮(用户控制范围内),将document.ready()不会被调用(document.load,onload也没有工作)
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.readynot working?
我在网上对此进行了研究并发现了类似的问题,但没有任何内容可以解释为什么这不起作用。document.ready不工作还有什么其他原因?
采纳答案by El Ronnoco
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...
这将是部分回发的问题。DOM 不会重新加载,因此文档就绪函数不会再次被触发。您需要像这样在 JavaScript 中分配一个部分回发处理程序......
function doSomething() {
//whatever you want to do on partial postback
}
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(doSomething);
The above call to add_endRequestshould be placed in the JavaScript which is executed when the page first loads.
上面的调用add_endRequest应该放在页面第一次加载时执行的 JavaScript 中。
回答by Pranay Rana
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
}
回答by tedski
Instead of $(document).readyyou could use function pageLoad(){}.
而不是$(document).ready你可以使用 function pageLoad(){}.
It's automatically called by the ScriptManageron a page, even on a postback.
它由ScriptManager页面上的 自动调用,即使在回发时也是如此。
回答by pharophy
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
正如 El Ronnoco 所说,我不久前遇到过这个问题,它必须与 DOM 不重新加载。但是,您可以简单地更改
$(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,您可以添加任意数量的处理程序。
回答by Kasun Koswattha
Most of the times, this is happening because of the Updatepanle. Just put postback triggersto the button and it will solve this.
大多数情况下,这是由于 Updatepanle 而发生的。只需将回发触发器放在按钮上即可解决此问题。
回答by Ernest
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>
回答by romi
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. so use Sys.Application.add_load(function(){}); instead of $(document).ready. This works perfectly for me :)
我也面临同样的问题,但我发现 jQuery $(document).ready 事件处理程序在页面加载时工作,但在 ASP.Net AJAX UpdatePanel Partial PostBack 之后它不会被调用。所以使用 Sys.Application.add_load(function(){}); 而不是 $(document).ready。这对我来说非常有效:)
<script>
Sys.Application.add_load(function() {
//Your code
});
</script>
<script>
Sys.Application.add_load(function() {
//Your code
});
</script>
回答by Reginardo Tribuzi Lula Júnior
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.
下面的代码很好地解决了这个问题。如之前发布的链接所示(http://encosia.com/document-ready-and-pageload-are-not-the-same/),当你有一个带有 updatePanels 的 asp.NET 时,你应该使用函数 pageLoad()。当您只有一页并且在每次回发时它将完全重新加载, $(document).ready() 是正确的选择。
Example using pageLoad:
使用 pageLoad 的示例:
function pageLoad() {
$(".alteraSoVirgula").keyup(function () {
code here
})
}

