【问题标题】:onbeforeunload and asp:Update Panel in Safari and FireFoxonbeforeunload 和 asp:更新 Safari 和 FireFox 中的面板
【发布时间】:2012-09-10 21:50:58
【问题描述】:

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

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

这里有一些代码sn-ps

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>

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

【问题讨论】:

    标签: javascript asp.net asp.net-ajax updatepanel


    【解决方案1】:

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

    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;  
    }
    

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

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

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

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

    我在找到答案之前就找到了这个问题,所以如果其他人有同样的问题,这可能会为他们节省一些时间。希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      相关资源
      最近更新 更多