Experts Round Table Network

Programming => .NET => Topic started by: rodri on February 02, 2006, 06:52:13 AM



Title: Avoiding Refresh
Post by: rodri on February 02, 2006, 06:52:13 AM
hello everyone,

I have a page that has a button, which inserts  a row in a DataBase. If after have pressed the button I refresh the page, the refreshing makes a new insert (which obviosly is not wished). I heard that I could avoid this situation using HTTP headers that disable the cache, but i couldn't figure this out until now.

Could anyone give a piece of advise?

thanks a lot.


Title: Avoiding Refresh
Post by: Anonymous on February 02, 2006, 07:31:44 AM
The way I understand the problem is that pressing the button makes a POST request to the webserver.

Refreshing the page after the results re-issues the POST request.

There are many techniques available to stop this.

For me, the most obvious one is to simple not inject repeat data. The application should be able to determine if the data has already been posted and to not repost it.

Another way is to pass the user a page which is basically a redirect to the results page.

If they then refresh, the redirect is the page being reloaded (or the results page) and not the original POST.

The cache controller available are not guaranteed in all cases and knowing that, relying on them is dangerous.


Title: Avoiding Refresh
Post by: IM on February 02, 2006, 07:59:26 AM
Using a session (or just some cookie) would seem to be one way around this.

But I'd personally go for RQuadling's suggestion of
"pass the user a page which is basically a redirect to the results page."


Title: Avoiding Refresh
Post by: VGR on February 02, 2006, 12:32:06 PM
Yes. In fact, as this is not a pure .Net question, it happens that it was asked - and answered I'm afraid - on this very same site, in the "web dev" section (or the like)

The "usual" solution is your script done this way :

// inits
// get $_POST[] data (for example)
// if the FORM was submitted
//    check data
//    if correct
//       write to DB
//       redirect to same page (thus clearing up the POST data, so that a F5/Refresh doesn't do anything
//    if not, complain
//    endif
// endif
// display FORM and current content


that's ONE solution, the one which works the best IMHO

An other solution is to play with the HTTP headers about "cache-control", but this is unreliable IMHO
In PHP you would use simply issue the line
header("Cache-Control: none, must-revalidate");
or the like, before any HTML is output.

This problem is also discussed in various places on EEE.org

regards


Title: Avoiding Refresh
Post by: rodri on February 03, 2006, 07:00:29 AM
well thanks for all the suggestions posted. I tried with the meta tags:
Code:

<meta http-equiv="expires" content="-1" />
<meta http-equiv="Cache-Control" content="no-cache;no-store;must-revalidate" />


but i didn't have any good result.

about my app, i got tree grids on the same page, each one makes a new insert with its corresponding button, thats why I can't send the user to another results page.

well, about VGR's suggestion, i understood that i shoud validate the data right?, but what shold be the best way:
 * to do this against a DataBase or
 * by comparing with a cookie.

I think that both have good & bad issues.

I'll try to solve this problem and I'll tell later.

thanks a lot.


Title: Avoiding Refresh
Post by: Esopo on February 17, 2006, 05:24:58 AM
Have you considered redirecting the page to itself before displaying the results so that all cache is lost?

Works like this:
1. A page request is made with POST (and/or GET)
2. The page process it and before showing the results it sends a blank page redirecting to itself
3. the page is reloaded and loses any cache. If the redirect was sent through server headers it happens instantly and the user won't notice a thing.

Alternatively you don't need to redirect to the same page. If for any reason you are having problems adapting the page to receive the self-redirection, you can just move the code to another page and redirect to that. The ultimate goal is to end with a page that did not receive any data so that users can't resend the form by refresing the page.

To send a redirect by headers with asp.net:
Response.Redirect("page2.aspx")

Hope that helps.


Title: Avoiding Refresh
Post by: SSirica on February 27, 2006, 12:43:52 PM
Sounds like a job for "Page.IsPostBack".  When it is a postback, ie pressing the button, save the record.  When it's not, don't.