C# 回发后,我的 JavaScript 函数在 ASP.NET 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9719078/
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
After postback my JavaScript function doesn't work in ASP.NET
提问by Mennan
I have common functions and I collapse it on CommonFunctions.jsin Scripts folder.
我有通用功能,并将其折叠CommonFunctions.js在 Scripts 文件夹中。
I include it on my master page and use it on my pages. When I do any post back on a page, my function doesn't work.
我将它包含在我的母版页上并在我的页面上使用它。当我在页面上回帖时,我的功能不起作用。
My CommonFunctions.js:
我的CommonFunctions.js:
$(function () {
gf();
if (Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) {
gf();
}
function gf(){
$('.AddNewGeneralPanel').click(function () {
if ($(this).find('.AddNewGeneralPanelStyle').text() == "") {
$(this).find('.AddNewGeneralPanelStyle').text("( Gizle )");
lastOpenId = $(this).attr("kodid");
}
else
$(this).find('.AddNewGeneralPanelStyle').text("");
$(this).next('.AddNewGeneralAccordionDiv').slideToggle('slow', function () {
});
});
}
});
采纳答案by Josh Earl
Since you're using an UpdatePanel, the part of the DOM that you've attached your event handler to is getting dropped and recreated after the postback. This has the effect of removing any event handlers that were attached by jQuery when the page first loaded.
由于您使用的是UpdatePanel,因此您将事件处理程序附加到的 DOM 部分将在回发后被删除并重新创建。这具有删除页面首次加载时由 jQuery 附加的任何事件处理程序的效果。
When you postback only part of the page, the jQuery $(function() {});doesn't fire again, so your handlers never get reattached.
当您仅回发页面的一部分时,jQuery$(function() {});不会再次触发,因此您的处理程序永远不会重新附加。
Here's a related questionthat shows how to resubscribe your events when the UpdatePanelrefreshes.
这是一个相关的问题,显示了如何在UpdatePanel刷新时重新订阅您的事件。
回答by PraveenLearnsEveryday
It is because of updatepanel partial postbacks. here is what you need to do.
这是因为更新面板部分回发。这是您需要做的。
function pageLoad(sender, args)
{
$(document).ready(function(){
// put all your javascript functions here
});
}
I had the same issue and it worked for me. I hope it helps you too.
我有同样的问题,它对我有用。我希望它也能帮助你。
回答by Stilgar
Assuming you are using an UpdatePanel to do the postback and the JS code is attatched to HTML elements within the update panel you need to attach them again when the update panel is loaded. You can hook up your code on the endRequest event:
假设您使用 UpdatePanel 进行回发,并且 JS 代码附加到更新面板中的 HTML 元素,您需要在加载更新面板时再次附加它们。您可以在 endRequest 事件上连接您的代码:
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler)
http://msdn.microsoft.com/en-us/library/bb383810.aspx
http://msdn.microsoft.com/en-us/library/bb383810.aspx
Keep in mind that if you are using more than one update panel you should check which update panel is refreshed and attach only the events that are needed otherwise you risk events firing more than once.
请记住,如果您使用多个更新面板,您应该检查哪个更新面板被刷新并仅附加需要的事件,否则您可能会冒事件触发多次的风险。
回答by user3783446
It is because of updatepannelused. Following code is working fine. Just put your jquerycode inside the pageLoadevent like below
是因为updatepannel用过。以下代码工作正常。只需将您的jquery代码放在pageLoad事件中,如下所示
function pageLoad(sender, args) {
$(document).ready(function () {....}
}
回答by Alberto Garcia E.
i have the same problem, i have solved with this code:
我有同样的问题,我已经用这个代码解决了:
<script type="text/javascript">
function pageLoad()
{
$('#datetimepicker2').datetimepicker();
}
</script>
回答by Singh T
Faced same problem. Change
面临同样的问题。改变
$(document).ready(function() {
to
到
Sys.Application.add_load(function() {
this will force it to run even on partial postbacck
这将迫使它甚至在部分回发时运行

