为什么OnBeforeUnload与ASP射击两次:LinkButton的? [英] Why is OnBeforeUnload firing twice with asp:LinkButton?
问题描述
我有以下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 :LinkButton的
,因为它需要在离开页面之前,火了一些服务器端code。当用户点击这个的LinkButton
他们得到及时的两倍。
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.
方案:
- 用户点击
的LinkButton
- 提示符出现和用户点击取消按钮。
-
提示符消失,用户仍然在屏幕上。不错。
- User clicks the
LinkButton
- Prompt appears and user clicks the cancel button.
Prompt disappears and user is still on screen. GOOD.
用户点击的LinkButton
那么,为什么我得到第二个提示?该OnBeforeUnload如何射击两次?
So why am I getting the second prompt??? How is the OnBeforeUnload firing twice?
推荐答案
我也有类似的问题,但是,我并不想火onbeforeunload为LinkButtons可言,所以我用了每一个LinkButton的上下面的.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"
在LinkButtons的功能并未被削弱,因为他们所做的一切与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射击两次:LinkButton的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!