Reputation: 1044
I have an asp.net application and when I refresh a web page which is produced after a form is submitted.
Example:
<form name="myform" action="mypage.aspx" method="post">
then the following alert is shown.
"The page cannot be refreshed without resending the information. Click Retry to Send the information again, or click Cancel to return to the page that you were trying to view."
With two buttons:
Retry Cancel
How can I avoid the above alert and take "Cancel" as default & refresh immediately?
Thank you
Jeff
Upvotes: 2
Views: 15465
Reputation: 39135
You should use the "Redirect-After-Post" pattern. Google for more info. But basically on form submission your server sends a redirect response to the client, which then fetches this page. This is the best practice for avoiding the issue you are seeing: you don't get that annoying message on page refresh, the back button is not "broken" and it prevents accidental double submission. Really every form on your site should implement this pattern.
Here are a couple of questions on implementing it in asp.net:
How to use ASP.NET with "Redirect after POST" pattern? [Edited]
Redirect After Post in ASP.NET MVC
Upvotes: 7
Reputation: 4864
Jeff,
To expand on the above comments:
Either switch the
method="post"
to:
method="get"
... or do this manually, in a way that does not compromise the security of ur data.
Say you have a login for with two textboxes user/pass. You could create a struct/class with this information, and then generate a random GUID.
do something like:
Session[GUID] = new MyPostInformationClass() {login = txtLogin.Text, pass = txtPass.Text};
and then:
Response.Redirect("mypage.aspx?guid=" + GUID);
then on the Page_Load:
MyPostInformationClass usr = (MyPostInformationClass)Session[GUID];
String login = usr.login;
String pass = usr.pass;
Of course this is an overly simplified example; but you can F5 you heart's desire.
Upvotes: 1
Reputation: 1086
Jeff,
I had this same issue yesterday with a multi-paged form (4 seperate steps). What I did in the end was created a "handler" file and used that to deal with posted data, then performed a redirect to the page I wanted. This counters the "resend" problem as data is only ever posted to a page that is dedicated to processing it.
My HTML Form:
<form action="handler.asp" method="post">
<input type="text" name="firstName" />
<input type="submit" value="Send Data" />
<input type="hidden" name="action" value="displayName" />
</form>
In my handler.asp:
<%
if Request.Form("action") = "displayName" Then
' Do whatever is needed here.
' After you're done
' Send the user where you want them to go
Response.Redirect("default.asp?status=thanks")
End If
%>
I hope this is useful. Good luck
Upvotes: 1
Reputation: 101614
As I mentioned in a comment, you can store the last submission in a variable (such as session), then use a redirect to re-visit the page. A "refresh" classically is just going to perform the last option, so now way of "refreshing without a submit".
But, depending on what you're trying to achieve, you could also use an UpdatePanel and "Refresh" the page (or, for all intents and purposes, make it appear as though it's refreshed).
EDIT: If the user presses F5, then prevent that action (if it's imperative). Perform whatever actions you need on the initial page visit, then auto-redirect them to a "Result" page. That way any refresh just refreshes a "static" (already-generated/action-performed) page.
Upvotes: 1