how to prevent data from sending if user press F5 or refresh in asp.net?

I had asp.net form app I have a bug when the user click F5 or refresh it will enter the data from last data entry .is their is away to Prevent sending data if user click click F5 or refresh?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It’s easy to reset a page to it’s initial state in ASP.NET by redirecting to itself. Here are 3 ways you can do it:

  1. Response.Redirect(Request.Path);

In which the path to the request is presented in the following form: /MyApp/MyFile.aspx

  1. Response.Redirect(Request.RawUrl);

In which not only is the path exposed, but also any querystring parameters like:
/MyApp/MyFile.aspx?foo=bar

  1. Response.Redirect(Request.Url.ToString());

In which not only is the path and querystring parameters exposed, but made available as an absolute reference in the form:
MyServer/MyApp/MyFile.aspx?foo=bar

Method 2

A common solution to this is called Post Redirect Get (PRG), where the browser is immediately redirected to a HTTP Get page after any post. See Post Redirect Get in asp.net for a web forms implementation.

Method 3

There are multiple ways to prevent this from happening. The simplest is to Response.Redirect to a another page, which can be refreshed without consequence.

// process form post
Response.Redirect("anotherpage.aspx");


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x