Beginning Active Server Pages

Outputting Data 2

Another great thing with ASP is you are not limited to simply VB - you can 'hard-code' HTML too. Take a look at this example:

'declare the variables
Dim bTest, sName, sVariable
'set their values...
bTest = True
sName = "Fred"
sVariable = "hello James"
<table border="1">
If bTest Then
    Response.Write sVariable
    Response.Write sName
End If

What this code does is display some text in a HTML table. If the variable bTest is true (which, in this instance is the case), sVariable is placed in between the <td> and </td> tags. Otherwise, sName is outputted there instead. Not sure exactly what is happening? Take a look in your browser. Then, right click and select View Source. What you should see is this:

<table border="1">
  <tr><td>hello James</td></tr>

Another useful feature is that If...Then...Else statements don't only apply to Visual Basic. You can use them to selectively output existing HTML. Let's convert a previous example to using this syntax:

<% If Request.QueryString("id") = "1" Then %>
  <p>This is some different text</p>
<% Else %>
  <p>This is text!</p>
<% End If %>

which does exactly the same thing. You might also find you want to simply output the value of a variable into some hard coded HTML. ASP provides a function for this too. Instead of using the <% and %> tags, use <%=VariableName%>. For example,

<p>Welcome, <%=sName%> to VB Web!</p>

would insert the value of sName into the HTML, and is the same as

<p>Welcome, <% Response.Write sName %> to VB Web!</p>

One final thing before we move on... When using the Response.Write statement you might find you want to include a quote (") within the string. You can do this in two ways. Either use the Chr(32) statement:

Response.Write "<p><font color=" & Chr(34) & "#FF0000" & Chr(34) & ">Welcome!</font></p>"

or simply replace the single quote with a double quote:

Response.Write "<p><font color=""#FF0000"">Welcome!</font></p>"

I'll leave it up to you to decide which is more elegant!

You might also like...


About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“If Java had true garbage collection, most programs would delete themselves upon execution.” - Robert Sewell