Monday, February 21, 2011

Understanding ASP Script


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 tag and a 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

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:

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.

No comments:

Post a Comment