处理表单提交后的刷新
禁止表单在刷新时重新提交的最佳方法是什么? !IsPostBack
不处理刷新。我尝试使用在初始提交时设置为 true 的 bool 变量,但每次加载页面时它似乎都会重置。
我读过一些有关 cookie 的内容,但我不确定如何设置它们以在页面之间传递。
我的设置是:
- Default.aspx (& Default.aspx.cs)
- Results.aspx (& Results.aspx.cs)
我是否像 SessionState 一样在 web.config 中设置 cookie?还有比 cookie 更好用的东西吗?
What is the best way to disallow a form to resubmit on a refresh? !IsPostBack
doesn't handle refreshes. I have tried using a bool variable that sets to true on initial submission, but it seems to be resetting every time I load the page.
I've read a bit about cookies but I'm not sure how to setup these up to pass between pages.
My setup is:
- Default.aspx (& Default.aspx.cs)
- Results.aspx (& Results.aspx.cs)
Do I setup cookies in web.config like SessionState? Is there something better to use than cookies?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为你可以看看 Post/redirect/Pattern PRG 模式
来自维基百科
它只是在成功发布后进行重定向,以便用户刷新会导致完成幂等 GET 而不是 POST。它还有一个额外的优点,即浏览器会“忘记”其历史记录中的 POST,这意味着后退按钮也不会提交。
甚至您可以在向数据库提交数据之前根据某些主键或唯一标识符检查数据是否已经存在。
但你确实检查了这个 PRG 模式,它可能对你有帮助。
响应重定向。在 ASP.NET 中
as i think you could look at Post/redirect/Pattern PRG pattern
from wikipedia
It simply consists in redirecting after a successful post, so that the user refresh results in an idempotent GET to be done rather than a POST. It has the additional advantage that the browser "forgets" about the POST in its history, which means the back button won't submit either.
even you can check before submitting the data with your database, whether the data already exists or not based on some Primary key or unique identifier.
but you do check this PRG pattern, it may helps you .
Response Redirect. in asp.net
最好的想法是为表单分配一个唯一的 ID(将其放入隐藏的表单变量中)。提交表单后,您可以跟踪该 ID 之前是否已被“使用”。如果不是,您允许提交继续,否则您拒绝它。
编辑:我应该清楚,您确实需要一个数据库才能正确执行此操作。您可以尝试使用 cookie 进行欺骗,但这并不是万无一失的,因为用户可能在其浏览器中禁用了 cookie。
The best idea is to assign a unique ID to the form (put it in a hidden form variable). When the form is submitted, you keep track of whether the ID has been "used" before. If not, you allow the submit to proceed, otherwise you reject it.
Edit: I should be clear that you really need a database in order to do this properly. You could try to finagle something with cookies, but this is not fool proof, as the user may have disabled cookies in their browser.
在表单中设置目标,点击提交后就会跳转到目标页面。执行该页面中的所有处理并重定向到结果页面。这可以是您用于提交的同一页面。刷新结果页面不会进行任何重新提交。
set the target in the form and when you click submit it will go to the target page. do what ever processing in that page and do a redirect to the result page. that can be same page you used for the submit. refreshing the result page wont do any resubmission.
对于这种情况,我觉得没有什么比下面的链接更好的了。
http://www.codeproject.com/Articles/18841/Refresh-Module
I feel nothing better than below link for this scenario.
http://www.codeproject.com/Articles/18841/Refresh-Module