gpt4 book ai didi

javascript - onbeforeunload 和 asp :Update Panel in Safari and FireFox

转载 作者:行者123 更新时间:2023-12-01 16:41:33 28 4
gpt4 key购买 nike

我正在开发一个asp.net(框架4),它要求用户在尝试离开页面而不保存更改时收到通知。好吧,我通过 JavaScript 调用 onbeforeunload 函数找到了解决方案。在 IE 中运行良好,但在 Firefox、Safari 或 Chrome 中运行不佳。

现在问题就出在这里。我正在使用 AJAX 回发,如果在离开页面之前提示用户保存更改,则更新面板内的任何控件将不再回发到服务器。例如,如果按下更新面板内部的保存按钮,则不会发生任何事情。

这里是一些代码片段

JavaScript:

window.onbeforeunload = function (e) {
if (doBeforeUnload() == true) {
var ev = e || window.event;
// For IE and Firefox prior to version 4
if (ev) {
ev.returnValue = "You have modified the data entry fields since the last time it was saved. If you leave this page, " +
"any changes will be lost. To save these changes, click Cancel to return to the page, and then Save the data.";
}
// For Safari
return "You have modified the data entry fields since the last time it was saved. If you leave this page, " +
"any changes will be lost. To save these changes, click Cancel to return to the page, and then Save the data.";

}
}

ASP.NET:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<%--no controls inside of this panel will postback to server if user clicks the “Stay on Page” button--%>
</ContentTemplate>
</asp:UpdatePanel>

网上好像有很多帖子,但没有解决方案。除了不使用异步回发之外还有其他IDE吗?

最佳答案

我在搜索类似问题时遇到了这个问题。我通过执行以下操作解决了这个问题:

var doOnBeforeUnloadCheck = true;  // global to set in link button
window.onbeforeunload = function (e) {
if (doOnBeforeUnloadCheck && doBeforeUnload()) {
// do stuff
}
// Make sure to always re-enable check to ensure the event will still trigger
// where not specified to disable event.
doOnBeforeUnloadCheck = true;
}

LinkBut​​ton 中,您只需确保在运行任何其他 javascript 或执行回发之前将全局设置为 false:

<asp:LinkButton ID="yourButton" runat="server"
OnClientClick="doOnBeforeUnloadCheck = false;" OnClick="yourButton_Click" />

我在以下链接的帮助下找到了这个解决方案:

http://www.jimandkatrin.com/CodeBlog/post/LinkButton-UpdatePanel-onbeforeunload.aspx

我在找到答案之前就发现了这个问题,因此如果其他人也有同样的问题,这可能会节省他们一些时间。希望有帮助。

关于javascript - onbeforeunload 和 asp :Update Panel in Safari and FireFox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12359967/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com