ASP Response Object : Write Method

The Write method writes any specified string to the HTTP output.

There is one mandatory argument:

  • Variant - the data to be written as a string.
The variant data type is the only data type available in VBScript. It can, however, hold many different subtypes, including Boolean, String, and Numeric. The Write method automatically converts the variant data type to the String subtype before sending it.


  dim helloVar
  helloVar = "Hello!<br>"

  Response.Write helloVar
  Response.Write "Hello!<br>"
The above example sends HTTP output in three different ways: regular HTML, response writing a variable, and response writing a String. It would result in the output:


Tips & Hints

The <%= %> Shortcut
When you are mixing HTML and ASP code on the same page, there are times when you want to send a variable you've set earlier to the HTTP output. Using Response.Write, you would have to write something similar to the following:

      <%Response.Write myVariable%>
However, ASP provides a shortcut to the Write method for sending single variables/strings to the output stream. You can use <%=variable%> in place of <%Response.Write variable%>. So the example from above becomes:
I find this extremely useful, because it saves time typing and the end result is more readable. However, you should be aware that neither of the above two examples are the most efficient way to send output. You see, in both of the examples above, you switch from HTML to ASP and back to HTML. This forces your web server to switch to the ASP script engine to process the ASP code and then back to normal to send the HTML, which causes a small performance hit. The most efficient way to send the above information would be to use only ASP code to write it. For example:
  Response.Write "<table>" & vbCrLf1
  Response.Write "<tr>" & vbCrLf
  Response.Write "<td>" & vbCrLf
  Response.Write myVariable
  Response.Write "</td>" & vbCrLf
  Response.Write "</tr>" & vbCrLf
  Response.Write "</table>" & vbCrLf
This is the most efficient code, but it is not the easiest to read. Herein lies your greatest tradeoff - readability or efficiency? If I'm not writing mission critical software which has to run as quickly as possible, I almost always take the small performance hit and use the inline shortcut. I find it makes it an order of magnitude easier to read and modify the code later on.

The println Function
When using lots of Response.Write's in an ASP page, I quickly get tired of writing "& vbCrLf" after every line of HTML to ensure a hard return in the source code (what you see when you Right-Click "View Source" in most browsers). Instead I write my own println subroutine to do it for me. It's a simple way around a simple problem.

An example:

  Sub println(printString)
    Response.Write printString & vbCrLf
  End Sub

  'now use println's in place of response writes when you want
  'to include a hard return after the line.

  myVariable = 200
  println "You have "
  println myVariable
  println " apples."
This results in the output:

You have 200 apples.

Notice that vbCrLf does not cause the HTML output to break lines. For this you still need to use HTML <br> and <p> tags. However, if you viewed the source of the page created by the above code, you would find that it looked like:
You have
While not particularly useful here, it is very useful when formatting long tables and lists.

Back to the ASP Response Object
Back to ASP Objects
1 Carriage Return, Line Feed. VBScript's newline. You can also use vbNewline.


All code is my own (yes, I actually claim it).

Log in or register to write something here or to contact authors.