||If you get a message similar to this one|
Response object error 'ASP 0156 : 80004005'
/somePageName.asp, line 9999
The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content.
then simply believe the message!
The rules of Web servers say that you can not do a
Response.Redirect once anything has been written to the browser!
"But I haven't done any Response.Write before this line!"
Ahh, but did you code even the tag <HTML> before the
Response.Redirect? That tag sends oodles of stuff to the browser, automatically!
So, what can you do?
(1) Don't do any Response.Write or put any ordinary HTML code or even put in the <HTML> tag until after the point where you do the (possibly conditional)
Most of the time, this is easy to do. For example, if you are validating a username and password, there is no need to output anything to the browser before the point where you discover that one or both of the values is wrong.
But if that doesn't work...
(2) Add the line
Response.Buffer = True
before any possible output on the page! Typically, it should be the first line in the file, barring any
<% @ .... %> line you might have.
Then, if your code decides it needs to do a
Response.Redirect, simply do this:
The call to
Response.Clear wipes out anything that was sitting in the buffer waiting to be sent to the user, at which point
Response.Redirect is much happier.
NOTE: Recent versions of IIS will turn buffering on by default, in which case the
Response.Buffer = True is unnecessary, though the call to
Response.Clear is still needed. However, it can't hurt to be sure buffering is enabled, so why not drop in that line, just to be safe?