问题
What is the best way to disallow a form to resubmit on a refresh? !IsPostBack
doesn't handle refreshes. I have tried using a bool variable that sets to true on initial submission, but it seems to be resetting every time I load the page.
I've read a bit about cookies but I'm not sure how to setup these up to pass between pages.
My setup is:
- Default.aspx (& Default.aspx.cs)
- Results.aspx (& Results.aspx.cs)
Do I setup cookies in web.config like SessionState? Is there something better to use than cookies?
回答1:
as i think you could look at Post/redirect/Pattern PRG pattern from wikipedia
The HTTP 1.1 specification introduced the HTTP 303 response code to ensure that in this situation, the web user's browser can safely refresh the server response without causing the initial HTTP POST request to be resubmitted.
It simply consists in redirecting after a successful post, so that the user refresh results in an idempotent GET to be done rather than a POST. It has the additional advantage that the browser "forgets" about the POST in its history, which means the back button won't submit either.
even you can check before submitting the data with your database, whether the data already exists or not based on some Primary key or unique identifier.
but you do check this PRG pattern, it may helps you .
Response Redirect. in asp.net
回答2:
The best idea is to assign a unique ID to the form (put it in a hidden form variable). When the form is submitted, you keep track of whether the ID has been "used" before. If not, you allow the submit to proceed, otherwise you reject it.
Edit: I should be clear that you really need a database in order to do this properly. You could try to finagle something with cookies, but this is not fool proof, as the user may have disabled cookies in their browser.
回答3:
set the target in the form and when you click submit it will go to the target page. do what ever processing in that page and do a redirect to the result page. that can be same page you used for the submit. refreshing the result page wont do any resubmission.
回答4:
I feel nothing better than below link for this scenario.
http://www.codeproject.com/Articles/18841/Refresh-Module
来源:https://stackoverflow.com/questions/8828847/handling-refreshes-after-form-submission