7

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?

  • 1
    @aelnajjar at least look before here! http://stackoverflow.com/questions/3759572/avoid-form-re-submit and http://stackoverflow.com/questions/2526568/asp-net-prevent-form-submission-twice this has been asked so many times! – Erre Efe Sep 10 '12 at 14:16

3 Answers3

15

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

zeco4ever
  • 313
  • 2
  • 9
6

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.

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285
1

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");
mellamokb
  • 56,094
  • 12
  • 110
  • 136