Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 202,528 times

Contents

Related Categories

Beginning Active Server Pages - Outputting Data 2

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">
  <tr><td><%
If bTest Then
    Response.Write sVariable
Else
    Response.Write sName
End If
%></td></tr>
</table>

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>
</table>

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!

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [1010] Beginning Active Server Pages - absolute beginner!

    Posted by michael poxon on 16 Dec 2006

        Hi,
    When I say I'm an ASP beginner, I mean it! I've only written one trivial bit of code, and got a blank page. I see now that's described in the article snippet below. I did ind...

  • .I want to know how to save the files.whats the extensions...

    Posted by writetoksk on 04 Oct 2006

    hi,


    its first i started ASP.I want to know how to save the files.whats the extensions...Where can i get the sample programs

  • Posted by James Crowley on 28 Dec 2004

    Are you running IIS ? And are you viewing it in your browser via the correct URL? (ie something starting with http:// rather than file:// ) ?

  • Posted by James Crowley on 28 Dec 2004

    It does - you just can't see it ;) We've got an ISAPI filter that rewritse /show/1010/ to something like /show.aspx?id=1010

  • It's not working ;_;

    Posted by HyperHacker on 12 Dec 2004

    Is there something special I have to do besides saving it as a .asp file, or does it just not work on my server? It just spits out the code, even HTML, as plain text.

    [code]
    Let's see if ASP...