简体   繁体   中英

Best Way to avoid Reinsertion of data in ASP.net on Page Refresh

I want to know what is the best way to avoid the reinsertion of data in ASP.net.

I am currently doing

Response.Redirect('PageURL');

Thanks in Advance

不要将插入代码放在Page_Load方法中,或者如果是,请确保首先检查Page.IsPostBack

Yes, normally we have an identity autoincrement number id, wich should be sent back to your form after the insertion. So you just have to check on server if that number is > 0 and execute an update instead of an insert .

Your redirect solution is valid. This pattern is called Post/Redirect/Get .

Post/Redirect/Get (PRG) is a web development design pattern that prevents some duplicate form submissions, creating a more intuitive interface for user agents (users). PRG implements bookmarks and the refresh button in a predictable way that does not create duplicate form submissions.

When a web form is submitted to a server through an HTTP POST request, a web user that attempts to refresh the server response in certain user agents can cause the contents of the original HTTP POST request to be resubmitted, possibly causing undesired results, such as a duplicate web purchase.

To avoid this problem, many web developers use the PRG pattern[1] — instead of returning a web page directly, the POST operation returns a redirection command. The HTTP 1.1 specification introduced the HTTP 303 ("See other") 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. However most common commercial applications in use today (new and old alike) still continue to issue HTTP 302 ("Found") responses in these situations. Use of HTTP 301 ("Moved permanently") is usually avoided because HTTP-1.1-compliant browsers do not convert the method to GET after receiving HTTP 301, as is more commonly done for HTTP 302.[2] However, HTTP 301 may be preferred in cases where it is not desirable for POST parameters to be converted to GET parameters and thus be recorded in logs.

http://en.wikipedia.org/wiki/Post/Redirect/Get

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM