The % is the Magic!

Now we get into the ASP part of our script. The key character is the %. Everything between the % tags is VBScript:

<% For iCounter = 1 to 5  
  Response.Write(iCounter) %>
  <BR>
<% Next %>
<HR>
</BODY>
</HTML>

All of the scripts we will be writing are written in VBScript. Everything enclosed between the <%  %> tags is Active Server Page code. The VBScript variety of server code, which we are now writing, can have the % tags sprinkled throughout a standard HTML page. And just like HTML tags, every script opening tag <% must have a corresponding script closing tag %>. VBScript is the default scripting language of Active Server Pages. Active Server Pages will process any legitimate commands that are between the <% and %> tags.

Here's the first chunk of script again:

<% For iCounter = 1 to 5  
  Response.Write(iCounter) %>

So we are writing VBScript code here between the % tags. Using VB syntax, we are just looping 5 times. For each iteration through the loop, we are writing the result of iCounter to the browser. We use the .Write method of the ASP Response object to send information to the browser. Whatever we write is treated as a variant. Notice that we did not dimension iCounter. Any variable defaults to a variant, just like in Visual Basic 6. So if you wrote a script that had a line such as:

<% Response.Write now %>

you would get the following result rendered on the browser:

7/19/98  11:50:22 PM

After we output the value of iCounter to the browser, we send the HTML tag <BR>, for a hard break. So we must delimit our VBScript code with the %> right after we send the response - Write now %>.

Here's the next part of this page's script component:

<BR>
<% Next %>
<HR>

Here we place the HTML <BR> tag which will place the next number produced by our loop right underneath. And then we need the ASP code Next to complement our For loop. So the Next keyword must be enclosed in <% %> because it is VBScript, not HTML. You get the idea. We bracket the ASP specific commands within the % tags and leave the standard HTML code out in the open. When each of the five numbers are printed, we place another <HR> tag to give a nice effect. And to wrap things up, we end our page - as all HTML pages are ended with this:

</HTML>

There, you have written your first ASP page. And that's all there is to it. Of course, we will get a bit fancier with the output in the next example, but you now have the essence of an ASP page.

You have probably noticed that HTML commands are really designed to specify the logical structure of the document, far more than the physical layout. For example, our HTML tags tell the browser what the heading should be by enclosing it within the <H1></H1> tags, but the browser can find its best way of displaying the heading. If we shrink or stretch the browser window, it is responsible for reformatting as best it can to adapt to the new size.

© 1998 by Wrox Press. All rights reserved.