Our First ASP Script

As mentioned, the best tool for writing ASP scripts is not Visual Basic, but Notepad.exe. Yep. We need to write these in ASCII and nothing beats Notepad (unless of course you have a copy of Microsoft Visual InterDev 6.0). But if you don't - no problem. Notepad.exe will serve our purposes, and it has the benefit of helping us clearly see what we're doing – some of the proprietary HTML editors complicate things with colors and automatic formatting 'behind our backs'. Notepad keeps things clear and simple, which is what we want when we're learning ASP.

Let's write that script.

Try It Out - Writing a Simple ASP Script

1.  Open up NotePad.exe – or any other text editor of your choice – and type in the following code. Save the file as FirstScript.asp. Be sure it is saved in your \Chapter13 directory. Remember, this is the directory where we'll ask PWS will go to find our files:

<HTML>
<HEAD><TITLE>Our First ASP Script</TITLE></HEAD>
<H1><B>Our First ASP Script</B></H1>
<BODY>

Let's count up to 5.

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

We will run this in a few minutes. First, we'll discuss some of the ins and outs of HTML and then talk about PWS some more.

 How It Works

To start out with, each page must start with the tag <HTML> and end with </HTML>. Our page is sandwiched between these tags. These tell the browser that this is a web page and, armed with this knowledge, it knows what to do and how to show it:

<HTML>
<HEAD><TITLE>Our First ASP Script</TITLE></HEAD>
<H1><B>Our First ASP Script</B></H1>
<BODY>

Let's count up to 5.

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

Everything between the <HEAD> and </HEAD> tags contain general information about the document, such as the title:

<HEAD><TITLE>Our First ASP Script</TITLE></HEAD>

Within these tags we place the <TITLE> and set it to Our First ASP Script. This is what shows up in the title of the browser. Titles should be descriptive because this is often used as a reference to visited sites. As a rule of thumb, you should be able to guess the contents of the page from the title alone.

Next comes the <BODY> tag:

<BODY>

Everything between this and the </BODY> tag encloses the HTML that is to be displayed in the browser, as well as our ASP code in our case here. Our ASP script is not dependent on the HTML tags. We are just adding them here for correctness. But we will see later on that some ASP specific commands come at the very top of the page.

Next we are using the <H1> tag:

<H1><B>Our First ASP Script</B></H1>

This stands for 'level-1 heading'. Headings come in six levels, H1 through H6, decreasing in importance. Next we sandwich our heading text Our First ASP Script between the <B> and </B> tags. This tells the browser to render any text between these two tags in bold. So, this HTML tells the recipient browser that we want to render a first level heading in bold text on the page.

Then we simply instruct the browser to display Let's count up to 5.We just type it in. Finally, we have a <BR> tag and a <HR> tag.

Let's count up to 5.

<BR>
<HR>

This is known as a Line Break and acts just like a hard carriage return. So the next thing rendered will be on the next line directly under the line with the <BR> tag. Lastly, for some gratuitous effects, we add the HTML tag <HR> for Horizontal Rule. This will draw a nice recessed line across the screen for us.

© 1998 by Wrox Press. All rights reserved.