We help IT Professionals succeed at work.

How to prevent the last action from executing when a page gets refreshed?

NaumLitvin
NaumLitvin asked
on
Hi

I have a GridView and DetailsView on same page. When I INSERT a record using DetailsView it gets inserted fine. But if I refresh the page in browser it repeats the Insert. How to prevent  that? Do I need to post any code? markup? code behind? both?

Thanks
Comment
Watch Question

Commented:
refresh the page (javascript) when the page is loaded, that way the last action was a refresh.

Commented:
go to sun java and upgrade java script
Commented:
.. or simply redirect the page after the data is added.

The alternative is to submit the data via ajax, that way the form would never really have been posted.
Commented:
You have maybe two options:
- more complicated is to build your own mechanism that will monitor postback parameters or postback operations on grid and which will refuse to make the same postback twice.
- much simplier option (which I use in my apps) is to call Response.Redirect(samePageUrl) after concrete postback operation is competed (for example GridViewRowUpdated) so that whole page is refreshed without posting any data. When user clicks page reload (F5) no other postback is called.

Author

Commented:
I probably did not explain the problem well, sorry...

1) I do not want it to repeat Insert when I hit F5. F5 should do nothing - just execute whatever is coded for Page Load.

2) I do not see any reasons to update my javascript - please explain why I should. It seems to be working fine on hundreds other pages...

thanks
Commented:
Simply call Response.Redirect(samePageUrl) after you complete your postback operations and then your page will refresh without any postback data.

Author

Commented:
Thank you guys. You are so good - you do not even understand how simple can be mind of a simple-minded dude. Your solution (redirect) works fine and my problem is solved - thank you very much. But your explanation flew right over my (half-full) head. It would be great of you can get down closer to Earth one notch (or two) and try to explain what was going on and what got changed with redirect.
But again, page is working, thanks a lot!!