Using Response.Redirect to Change Pages

The response.redirect command in ASP helps you automatically redirect to a new page if you need to. It’s great for showing Thank You pages or moving through form processing.

Let’s say you have a contact form, that asks a user for their email address and the text of their message to you. You can see the basics of ASP form creation in the Making an ASP Form page.

So let’s say you send this form along to the page FormProcess.asp to do the processing. In this page, you take in the email address and message, and mail it off to your webmaster account for review. So far so good. If you actually want to learn how to do this, I have an Article on Mailing with ASP on the site.

But now that the mail message is sent, you want to say thank you to your user! It took them time and effort to write that comment to you, even if it was a short comment. The best way to do this is, at the end of your FormProcess.asp script, to send that user automatically to a thank you page. You would do that with the Response.Redirect command.

The most important thing to understand with this command is that you CANNOT have put any HTML at all on the screen before you start redirecting elsewhere. So in our example here, the user got a page with a form. They filled out the page and hit SUBMIT. They were sent to FormProcess.asp which was a processing page. This had ASP code in it but it did NOT WRITE ANY HTML. It didn’t put a HEAD or BODY or anything else on the page with a response.write command. It simply took in the email address and message and created a mail message with it.

Once that processing is done, you can then say

response.redirect(“thankyou.asp”)

the script will then merrily launch the thankyou.asp page, which can show your thank you message and let them continue to explore your site.

Remember, the user physically LEAVES YOUR FIRST SCRIPT when they hit that redirect command. So don’t have any other commands beneath it! They will not get executed. Make sure all of your connections are closed and other cleanup is done before you send the user to the new page.

If you get an error that resembles:

Response object error ‘ASP 0156 : 80004005
Header Error
/index.asp, line 22
The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content.

then read more about The HTTP Headers Error

Also, you might want to read How Buffering Works in ASP

ASP Basic Concepts