asp.net prevent表单提交两次 [英] asp.net prevent form submission twice

查看:119
本文介绍了asp.net prevent表单提交两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web窗体的Web应用程序(asp.net 2.0)。当用户提交我已经提交链接居然要离开的形式,使他们不能再次提交。然而,他们可以preSS F5并导致另一插入到数据库中,我不希望有发生。

I have a web forms web application (asp.net 2.0). When the user submits the form I have the submit link actually going away so they can't submit it again. However, they could press F5 and that is causing another insert into the database, which I don't want to have happen.

有一些,我可以设置如果设置/当他们这样做preSS F5告诉页面 - ?不要再

Is there a setting of some sort that I can set if/when they do press F5 to tell the page - don't submit again?

推荐答案

这听起来像你做回发到同一个页面,然后重新显示在同一页面给用户之前插入到数据库中,减去提交按钮?

It sounds like you're doing a postback to the same page, then inserting to a database before redisplaying the same page to the user, minus the submit button?

如果是这样的话,在你的问题,最简单的方法是做一个的Response.Redirect()到同一页面 - 这样用户presses时F5这将刷新GET请求的页面,而不是POST。

If this is the case, the easiest way around your problem is to do a Response.Redirect() to the same page - so when the user presses F5 it will reload the GET request to the page instead of the POST.

当您在ASP.NET做回传,浏览器会发送一个 POST请求到服务器。我不知道你的HTTP方法的背景,但即使你是新来的网络编程,你可能已经经历了这些类型的浏览器请求的影响。有时候当你在网站上提交表单,并击中了后退按钮,它会说类似你想重新提交表单数据。这是因为那些形式是使用HTTP POST方法将数据发送到服务器。在这种情况下,回退按钮的效果是一样的时,preSS F5 - 浏览器上再次重复完全相同的POST请求(这需要某种类型的形式的数据,并且在这些情况下,它是相同的数据时提交的最后时间)。

When you do a postback in ASP.NET the browser will send a POST request to the server. I don't know your background in HTTP methods, but even if you're new to web programming you've probably experienced the effects of these types of requests in browsers. Sometimes when you submit forms on websites and hit the back button, it will say something like "would you like to resubmit the form data". This is because those forms are sending data to the server using the HTTP POST method. The effect of the back button in this case is the same as when you press F5 - the browser is repeating the exact same POST request over again (which requires some type of form data, and in these cases it's the same data you submitted the last time).

当您使用的Response.Redirect(),服务器发出 HTTP状态code 302 (资源临时移动)响应该请求。然后,浏览器,现在将发出它已被告知要重定向到资源的GET请求(在此情况下,你的完全相同的页面)。

When you use Response.Redirect(), the server is issuing HTTP status code 302 (resource moved temporarily) in response to the request. The browser then, will now issue a GET request for the resource it has been told to redirect to (in this case, your same exact page).

现在,当你在页面上,就好像你已经从一个链接访问,或直接键入它到地址栏返回。如果preSS F5这个时候,而不是一个POST GET请求会发出。

Now when you're on the page, it's returned as if you had visited it from a link or typed it directly into the address bar. If you press F5 this time, a GET request will be issued as opposed to a POST.

我建议你下载萤火并激活网面板。您可以看到究竟是什么组成的所有请求的列表沿着请求/响应被你的网页上进行的。

I would recommend you download Firebug and activate the "Net" panel. You can see exactly what composes a request/response along with a list of all the requests being made on your page.

如果你喜欢冒险的感觉,你可以检查出更地位codeS (和通过其实我已经在我的工作作为一个黑客绕过应用程序中使用418的方式形成了ASP.NET验证)。

If you're feeling adventurous you can check out even more status codes (and by the way I have actually used 418 in an application I'm working on as a hack to get around Forms Authentication for ASP.NET).

如果Firebug是不够的,你可以下载提琴手居然手工制作和重播请求以自己的喜好。

If Firebug isn't enough, you can download Fiddler and actually handcraft and replay requests to your liking.

如果GET和POST是不够的,你可以用其他的方法来改变应用程序中的状态。你可能听说过 RESTful架构,基本使用HTTP方法GET,PUT,POST,并删除因为他们的目的。

If GET and POST aren't enough for you, you can use other methods to change states in your application. You may have heard of RESTful architecture, which basically uses the HTTP methods GET, PUT, POST, and DELETE as they were intended.

这篇关于asp.net prevent表单提交两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆