Tim
Tim

Reputation: 776

How can I "Go Back" without using Response.Redirect

I have a html form that I process with a aspx page. I want to be able to go back to my html form if the validation on the aspx page returns false (I cannot use javascript to validate the html form). I can't use Response.Redirect as I will lose the data initially entered on the html form.

The set-up is:

if values are invalid, go back to form.html, otherwise Response.Redirect("success.html")

Perhaps I am just going about this the wrong way. Could anyone suggest a better method?

Please note: I cannot use javascript on the form.html, however I can use javascript on the processform.aspx page - although I would prefer an alternative if possible.

Upvotes: 1

Views: 5163

Answers (7)

user153923
user153923

Reputation:

Just adding this so I can find it later.

protected void Page_Load(object sender, EventArgs e) {
  var employee = RequestPermission();
  if (!employee.Authorized) {
    Response.Redirect(Request.UrlReferrer.ToString());
  }
  if (!IsPostBack) {
    //Load the grid for the initial page
  }
}

This does not use JavaScript (unless the underlying Request.UrlReferrer is written in JavaScript).

NOTE: RequestPermissions is a tool our company uses to match up employee information using HttpContext.Current.Request.LogonUserIdentity.Name with a list of employee access information on our central server.

Upvotes: 0

spoulson
spoulson

Reputation: 21591

I would look at Server.Transfer. It will internally reroute the request to another page without a redirect. Use this to transfer to the page containing the form HTML.

Upvotes: 1

Svante Svenson
Svante Svenson

Reputation: 12478

Merge the two files into one, and show posted values in the form. Eg. <input type="text" name="lorem" value="<?=Request.Form["lorem"] ?? "default value" ?>">

If you have the form in a "dumb" html-file, you will not be able to inform the user of any validation errors, thus if the user enters incorrect data and submits the form they will get back to the form without any information whatsoever on what went wrong, and that's a no-no!

Upvotes: 0

o.k.w
o.k.w

Reputation: 25810

How about posting the form to another target? E.g. form target="_blank"

In the new window, perform the form data processing and then manipulate the html form page depending on the result.

It's untidy, but might just work for you.

PS: Do note that form target attribute is deprecated in HTML 4.01.

Upvotes: 0

user111013
user111013

Reputation:

You say that form.html can't use any javascript on form.html - does that mean you can't use Javascript at all in this solution?

My answer would be to output a javascript snippet from your processform.aspx that simply calls history.go(-1).

Example:

if(valid) 
{
    Response.Redirect("success.html", true);
} 
Response.Clear();
Response.Write("<html><body><script>history.go(-1);</script></body></html>");
Response.Flush();
Response.End(); 

If, however, you cannot use Javascript at all... the options are rather limited.

RFC 2616 defines HTTP 204 "No Content" - you could attempt to send that, but this is a success status, and behaviour from browsers might vary. Theoretically, however, the content should stay the same. Users might be very confused, however, because there's no visible feedback.

If you could explain the reasoning behind these restrictions, perhaps we could produce a better solution.

Upvotes: 2

NickFitz
NickFitz

Reputation: 35061

My approach would be to have the form submit to itself - that is, <form action="" method="POST">. The form, if retrieved using GET, simply displays itself. If it is retrieved using POST, it validates the posted information and, if there are any problems, re-displays itself as for GET, but with the errors flagged (and with the user's input preserved). If there are no validation errors, it does whatever it does with the information, and then uses Response.Redirect() to send the user to whatever the next page after a successful submission may be.

Upvotes: 1

User
User

Reputation: 30985

If you are using MVC, you will have to refill the model from the submitted values in Request.Form.

If you are doing the classic WebForms, you need to generate your form.html dynamically inserting the previously submitted values into it.

You could put your submitted values into session:

Session["UserName"] = Request.Form["UserName"]

Then you do redirect to your form.aspx:

<input type="text" value="<%= Session["UserName"]" />

This will however return the page as form.aspx. But it shouldn't be a real problem, it's just the name.

Upvotes: 1

Related Questions