防止按 F5 时刷新页面
我已经使用服务器端控件(如按钮)创建了表单..并且还在其上编写了事件.. 现在运行时,单击按钮后,我按 F5 刷新了页面。Page_load 执行正常,但 button1_click() 事件也触发...那么在这种情况下我如何停止此事件执行。请建议我
I have Created form with Server Side control like button..and also have written event on that..
Now runtime after click on the button i have refreshed the page by pressing F5.Page_load is executing fine but button1_click() event also firing...So how can i stop this event execution in this scenario.Please suggest me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
简短回答:不可能
较长回答:没有网站可以阻止浏览器的功能,因为这对浏览器来说是一个严重的安全问题。
Short Answer: Not possible
Longer Answer: No web site can block the browser's functionality as that would be a serious security concern for the browser.
欢迎来到网页开发,没有办法阻止页面刷新和转发,这完全是浏览器行为。
不过也有解决办法;您可以做的一件简单的事情是,在处理按钮点击后,您可以将浏览器重定向回页面,这样刷新就不会包含按钮按下的重新发布。显然,这可能需要一些其他状态管理系统(而不是视图状态),也许您可以将状态存储在会话中。
Welcome to web development, there is no way to stop the page from refreshing and reposting, this is entirely a browser behavior.
There are work arounds though; one simple thing that you can do is after processing your button click, you can redirect the browser back to the page so that a refresh will not include a repost of your button press. Obviously this might require some other state management system (rather than view-state), maybe you can store your state in the Session.
将此脚本发布到您的主页或您想要阻止 F5 按钮的位置。
Post this script on your master page or where u want to block F5 button.
您可能会使用:
这将显示一条提示,询问用户是否想要离开,但可能不需要。 (另外,我不知道支持的程度)
You could potentially use:
This will display a prompt asking the user if they want to navigate away and may not be desired. (Plus, I don't know the extent of support)
海报使用的是 ASP.NET WebForms。在 MVC 中,使用 Post-Redirect-Get 模式,这不会成为问题。
但自 2004 年以来,Webforms 已经解决了这个问题(甚至更早 - 我在 2001 年编写了一个简单的票证解决方案),这篇文章如下: http://msdn.microsoft.com/en-us/library/ms379557.aspx
基本上,作者创建了与每个请求关联的唯一票证,并删除了该票证的有效性第一次回发时的票证。任何意外(或恶意)的后续回发都会被忽略。它包含在一个基本页面中,因此应该很容易添加到任何 Webforms 解决方案中。
The poster is using ASP.NET WebForms. In MVC, using the Post-Redirect-Get pattern, this would not be a problem.
But this has been solved for Webforms since 2004 (and even before that - I wrote a simple ticket solution in 2001) with this article: http://msdn.microsoft.com/en-us/library/ms379557.aspx
Basically the author creates a unique ticket associated with each request, and removes the validity of the ticket on first postback. Any accidental (or malicious) subsequent postbacks are ignored. It's wrapped up in a base page so it should be easy to add to any Webforms solution.
您想要阻止的并不是真正的页面刷新,而是页面重新
POST
。如前所述,这在客户端是不可能的,因为刷新将重播浏览器在上次操作中执行的操作。
但是您可以将其捕获在服务器端,因为服务器可以保存一些信息来控制请求。
最简单的方法是将某种 RequestID 放在
Page Load
上的隐藏字段中,将其存储在Session
中的某个位置。并在下一个POST
上控制其值以验证请求,然后覆盖它以避免使用相同的 RequestID 重新POST
。也许有更好的方法:)
您的“问题”类似于用户双击恐怖行为: ASP.Net双击问题
Its not really page refresh that you want to block, its page re-
POST
.As already said, it's not possible on the client side because the refresh will replay what the browser did on the last action.
But you can trap it on the server side because the server can hold some infos to control the request.
The easiest way is to put a sort of RequestID in a hidden field on
Page Load
, store it somewhere like in theSession
. And control its value on nextPOST
to valid the request then override it to avoid re-POST
with the same RequestID.Maybe there's a better way :)
Your 'problem' is similar to a user double-click horror behavior : ASP.Net double-click problem
单击按钮时是否必须刷新页面?您可以重新连接按钮以进行 ajax 调用来提交数据吗?如果是这样,那么您就不必担心刷新时页面重新提交。
Do you have to refresh the page on the click of the button? Could you rewire the button to make an ajax call to submit the data? If so, then you would not need to worry about the page re-submitting on refresh.
F5 刷新问题的唯一解决方案是 Response.Redirect。请参阅我关于此主题的帖子。
https://stackoverflow.com/questions/12596152/asp- net-f5-browser-refresh-duplicate-record/12596153#12596153
The only solution for the F5 refresh problem is
Response.Redirect
. Please see my post on this topic.https://stackoverflow.com/questions/12596152/asp-net-f5-browser-refresh-duplicate-record/12596153#12596153
我得到一些不允许您按 F5 和/或 Ctrl + R 的代码:
I got some code that doesn't allow you to press F5 and/or Ctrl + R: