New row inserted for each page refresh

后端 未结 5 998
别那么骄傲
别那么骄傲 2021-01-15 03:27

Hi I\'m getting a strange problem while inserting records into database. In my button click event I\'m trying to insert some values into my database it is working fine. Once

5条回答
  •  忘掉有多难
    2021-01-15 03:53

    When you click the button, it sends a POST request to the server, and the updated page is sent back as the response. In order to refresh that page, the same POST must be made again, and the server interprets this as another button click.

    Most Web browsers give a warning in this situation, saying that refreshing the page may repeat any action that was just performed (in your case, inserting a row in the database). But if you want to prevent this from happening at all, the best way is probably to respond to the POST request with a redirect. In ASP.NET with C#, the way to do this is:

    Response.Redirect(url);
    

    Redirecting back to the same page is fine, or you could also redirect to a different page. When the browser receives this redirect, it will issue a GET request for the specified page. Then if you refresh, no action will be taken.

提交回复
热议问题