为什么 OnBeforeUnload 使用 asp:LinkBut​​ton 触发两次? [英] Why is OnBeforeUnload firing twice with asp:LinkButton?

查看:22
本文介绍了为什么 OnBeforeUnload 使用 asp:LinkBut​​ton 触发两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 Jquery 函数,可以在用户有未保存的更改(类似于 SO 的做法)并试图离开当前屏幕时通过提示通知用户.

I have the following Jquery function to notify the user with a prompt when they have unsaved changes (similar to how SO does it) and are trying to leave the current screen.

<!-- in my aspx page -->
<script type="text/javascript" language="javascript">
    window.onbeforeunload = function()
    {
        if (HasPendingUpdates())
        {
            return "Changes you have made will not be saved.";
        }
    }
</script>

这按预期工作,并在用户尝试单击 href 浏览或使用后退按钮等时显示消息框.我遇到的问题是我的一个链接是 asp:LinkBut​​ton 因为它需要在离开页面之前触发一些服务器端代码.当用户点击这个 LinkBut​​ton 时,他们会收到两次提示.

This works as expected and shows the message box when the user tries to click a href to browse away or use the back button etc. The problem I am having is that one of my links is a asp:LinkButton because it needs to fire off some server side code before leaving the page. When the user clicks this LinkButton they get the prompt twice.

场景:

  1. 用户点击LinkBut​​ton
  2. 出现提示,用户点击取消按钮.
  3. 提示消失,用户仍在屏幕上.很好.

  1. User clicks the LinkButton
  2. Prompt appears and user clicks the cancel button.
  3. Prompt disappears and user is still on screen. GOOD.

用户点击LinkBut​​ton

那为什么我会收到第二个提示???OnBeforeUnload 如何触发两次?

So why am I getting the second prompt??? How is the OnBeforeUnload firing twice?

推荐答案

我遇到了类似的问题,但是,我根本不想为 LinkBut​​tons 触发 onbeforeunload,因此我对 .aspx 上的每个 LinkBut​​ton 使用了以下内容页面:

I had a similar problem, however, I didn't want to fire onbeforeunload for LinkButtons at all, therefore I used the following for every LinkButton on the .aspx page:

OnClientClick="eval(this.href);return false"

LinkBut​​tons 的功能并没有被削弱,因为它们所做的只是提供一个带有 CommandArgument 的 Command 以供代码隐藏处理......

Functionality of the LinkButtons wasn't crippled, since all they did was supplying a Command with CommandArgument for the codebehind to handle...

这篇关于为什么 OnBeforeUnload 使用 asp:LinkBut​​ton 触发两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆