【问题标题】:How to stop firing onbeforeunload function on RadGrid Postbacks in ASP.NET如何在 ASP.NET 中的 RadGrid 回发上停止触发 onbeforeunload 函数
【发布时间】:2012-10-09 13:36:37
【问题描述】:

场景:我在.aspx 页面上有onbeforeunload 函数,用于在离开当前页面(ASP.NET 应用程序)之前显示一条消息。这个页面非常复杂,回发发生在很多次,例如我有多个按钮、Rad Grid、Rad Window PopUps 等。

因此,所有按钮在被点击时都会调用onbeforeunload,并且消息框会被不必要地调用。为了防止显示onbeforeunload 消息,我只是在按钮的OnClientClick 事件上调用另一个cancelEvent 函数,我只是调用null。通过这样做,所有按钮回发都得到处理,但我使用的 RadGrid 具有按钮列(删除),每当从网格中删除一行时,它都会刷新最终调用 onbeforeunload 的网格。

当我从 RadGrid 中删除任何内容时,谁能告诉我如何防止onbeforeunload。以下是我正在使用的代码 sn-p,并且在按钮上工作正常,但我需要网格删除列的帮助可能是一些事件,例如 Button 的 OnClientClick 事件,我可以用它来调用我的 cancelEvent

window.onbeforeunload = confirmExit;
function confirmExit() {
            window.event.returnValue = 'If you navigate away from this page any unsaved changes will be lost!';
}

function cancelEvent(event) {
            window[event] = function () {null}
}

<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" CssClass="ActionButtons"
            ValidationGroup="vsDataEntry" OnClientClick="return cancelEvent('onbeforeunload')"/>

【问题讨论】:

    标签: asp.net asp.net-ajax dom-events radgrid onbeforeunload


    【解决方案1】:

    尝试执行以下操作

    <div onclick="return cancelEvent('onbeforeunload')">
        <telerik:RadGrid runat="server">
        </telerik:RadGrid>
    </div>
    

    说明

    当点击事件被触发时,它会在 DOM 中的所有元素中冒泡。我们将 Radgrid 包装在一个 div 内,每当在该 div 内发生任何点击时,我们都会删除 onbeforeunload。

    【讨论】:

    • @user1273701 我还添加了解释为什么会这样。
    • +1 巧妙的技巧,知道跨浏览器的支持程度如何?
    • @eglasius 它在 Windows 中的最新版本的 Chrome、firefox、IE 上对我有用。
    猜你喜欢
    • 2012-10-02
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 2019-01-11
    • 2022-01-07
    • 1970-01-01
    相关资源
    最近更新 更多