为什么OnBeforeUnload与ASP射击两次:LinkBut​​ton的? [英] Why is OnBeforeUnload firing twice with asp:LinkButton?

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

问题描述

我有以下jQuery函数与当他们有未保存的更改(类似于怎么这么不吧)迅速通知用户,并试图离开当前屏幕。

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>

这工作正常,并显示消息框,当用户试图点击A HREF浏览掉或使用后退按钮等。我遇到的问题是,我的环节之一是 ASP :LinkBut​​ton的,因为它需要在离开页面之前,火了一些服务器端code。当用户点击这个的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?

推荐答案

我也有类似的问题,但是,我并不想火onbeforeunload为LinkBut​​tons可言,所以我用了每一个LinkBut​​ton的上下面的.aspx页:

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提供命令为codebehind处理...

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天全站免登陆