Why is OnBeforeUnload firing twice with asp:LinkButton?(为什么 OnBeforeUnload 使用 asp:LinkButton 触发两次?)
问题描述
我有以下 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:LinkButton
因为它需要在离开页面之前触发一些服务器端代码.当用户点击这个 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?
推荐答案
我遇到了类似的问题,但是,我根本不想为 LinkButtons 触发 onbeforeunload,因此我对 .aspx 上的每个 LinkButton 使用了以下内容页面:
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 的 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:LinkButton 触发两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!