I'm interested in implementing PRG in my website for some forms I've created. At present they postback to themselves, and obviously refreshing these pages posts the data in duplicate. Can anyone point me in the direction of a good tutorial of how I can code this into my site? I understand the logic but am not sure exactly where to start. Thanks
Asked
Active
Viewed 4,022 times
1 Answers
7
After you postback to the form you simply need to perform a redirect after the postback.
DoPostbackProcessing();
Response.Redirect("FormConfirmationPage.aspx");
As a very simple example, basically as long as you redirect (GET) to another page then the user cannot duplicate the postback. Of course if there are any errors in the forum you may not want to re-direct, but this is down to individual requirements.
EDIT: A good example of this is search, instead of posting back and then performing the search you would redirect and GET:
// Instead of performing search now we will redirect to ourselves with the criteria.
var url = "SearchPage.aspx?criteria=" + txtSearch.Text;
Response.Redirect(url);
This then redirects, the page then checks for a criteria query string and THEN performs the search, and when the user refreshes it searches again - plus they can bookmark the page for instant searching.

Mantorok
- 5,168
- 2
- 24
- 32
-
So how can I then refer to values from the original form that have then been posted to the database? In classic asp where I've done this kind of thing I'd use request.form("controlID"). Can I do this once the page has been redirected? – ajguk Mar 01 '11 at 10:20
-
No, the idea of PRG is to place key values in the urls query string, you can only retrieve form values during the post-back, the GET will use the query string. HTMS. Check this out too:http://en.wikipedia.org/wiki/Post/Redirect/Get. – Mantorok Mar 01 '11 at 10:25
-
Thanks for that, I've used request.querystring("blahblah") to grab the value. Out of interest, what if I had several values I wanted to insert into the url that exceeded the limit? Some of my forms transfer quite a lot of data. – ajguk Mar 01 '11 at 10:30
-
You should not submit the form this way, you still perform the post-back and update the database, but afterwards make sure you redirect OFF the page, this ensures that the users cannot double-post by mistake, the GET is exactly what it says, a GET, not a POST. – Mantorok Mar 01 '11 at 10:34