处理表单提交后的刷新

发布于 2024-12-26 11:16:47 字数 347 浏览 0 评论 0原文

禁止表单在刷新时重新提交的最佳方法是什么? !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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

末蓝 2025-01-02 11:16:47

我认为你可以看看 Post/redirect/Pattern PRG 模式
来自维基百科

HTTP 1.1 规范引入了 HTTP 303 响应代码
确保在这种情况下,网络用户的浏览器可以安全地
刷新服务器响应而不导致初始 HTTP POST
请求重新提交。

它只是在成功发布后进行重定向,以便用户刷新会导致完成幂等 GET 而不是 POST。它还有一个额外的优点,即浏览器会“忘记”其历史记录中的 POST,这意味着后退按钮也不会提交。

甚至您可以在向数据库提交数据之前根据某些主键或唯一标识符检查数据是否已经存在。

但你确实检查了这个 PRG 模式,它可能对你有帮助。

响应重定向。在 ASP.NET 中

as i think you could look at Post/redirect/Pattern PRG pattern
from wikipedia

The HTTP 1.1 specification introduced the HTTP 303 response code to
ensure that in this situation, the web user's browser can safely
refresh the server response without causing the initial HTTP POST
request to be resubmitted.

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

念三年u 2025-01-02 11:16:47

最好的想法是为表单分配一个唯一的 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.

在巴黎塔顶看东京樱花 2025-01-02 11:16:47

在表单中设置目标,点击提交后就会跳转到目标页面。执行该页面中的所有处理并重定向到结果页面。这可以是您用于提交的同一页面。刷新结果页面不会进行任何重新提交。

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.

演出会有结束 2025-01-02 11:16:47

对于这种情况,我觉得没有什么比下面的链接更好的了。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文