Wildfire Home Image [4k]

Starting ASP Programming

This article follows on from the article An Introduction to Active Server Pages. This article will introduce the basics of Server-Side scripting using VBScript. Some familiarity with the VB language is helpful, but the examples should be explained sufficiently for use by those who have no previous programming experience.

Contents

The Basic Structure of an ASP

An ASP can be broken down into two main sections:

  • Programmed code to be executed on the server
  • Data that is sent to the client "as-is" for processing

The code that is executed on the server is VBScript (for this tutorial) and the code that is sent to the client is standard HTML. The ASP processes the server-side code and then based on decisions that the code makes selects code to send to the client. This means that pages can be customised for the user at the server, sending a simple HTML page to the user.

Starting an ASP

There is only one bit of code that is essential to include in every ASP. This is the code that tells the server what language the page is programmed in:

<%@ LANGUAGE="VBSCRIPT" %>

This is all that is needed. The rest of the page can now be filled with HTML code to send to the client with VBScript code to perform calculations and actions. The tags that the Active Server Script resides within are the "<%" and "%>" tags. Anything between these tags (apart from VBScript comments) is parsed and acted upon by the server.

How to Comment Your Code

All developers want to comment their code at some point so that they can remember the particulars of the way their applications work. This is very easy to do. All you need is an apostrophe " ' " before any text that you want ignored:

<% ' this is a comment%>

Hello World

As with any new language the first tutorial will demonstrate how to do a "Hello World" type function, not to break with tradition here is a small example of how to write several "Hello World"s to the client:

<%@ LANGUAGE="VBSCRIPT"%>
<HTML>
<HEAD>
<TITLE>Hello World</TITLE>
</HEAD>
<BODY>
<%
'Initialise the variable called theMessage to store the sentence we are going to write
theMessage = "Hello World"
'Now write this to the clients browser using the ASP command response.write
response.write(theMessage & "<BR>")
%>

<P>The above message was done by using response.write. The below message is done using a more simple way of writing to the client</P>

<%= theMessage%>

</BODY>
</HTML>

Go through the above script to make sure that you understand it. The first section of code makes the variable "theMessage" store the value "Hello World". It then uses an ASP method "response.write" to write out the contents of "theMessage" and an HTML line break to the client. A simple HTML paragraph is then included. The next snippet of ASP code, note the equals sign (=) simply writes out the contents of "theMessage" to the client. You have now learnt two ways of writing custom data to the client, now to take it to a slightly more advanced level:

A Simple VB Script Page

To start off your VBScript/ASP programming here is a sample piece of code that will loop through various heading sizes:

<%@ LANGUAGE="VBSCRIPT" %>
<HTML>
<HEAD>
<TITLE>Demo Page</TITLE>
</HEAD>
<BODY>

<% for times = 1 to 3%>
<H<%= times%>>This is a heading size <%= times%></H<%= times%>
<% next%>
</BODY>
</HTML>

This will generate a custom HTML page to be sent to the client. The HTML that the client's browser will recieve will be:

<HTML>
<HEAD>
<TITLE>Demo Page</TITLE>
</HEAD>
<BODY>

<H1>This is a heading size 1</H1>
<H2>This is a heading size 2</H2>
<H3>This is a heading size 3</H3>

</BODY>
</HTML>

This code will be rendered on the screen without the client ever knowing what processing there was on the server. What the ASP code does is take the variable "times" and set it to be "1" it then writes out the heading code inserting the variable (a number) in the places where the number appears in the HTML code: in the <HX> tags and in the text. The code then loops, at the point "next", incrementing the variable "times" by one and looping through the code until "times" is "3". It then just sends out the rest of the page as normal.

In Summary

In this article you have learnt how to code a basic ASP page. Hopefully you will understand the VBScript, even if you are unfamiliar with general programming altogether. If you have any basic ASP questions don't hesitate to email me, if I can't answer the question myself then I'll point you in the right direction.

Microsoft have released a new version of ASP: ASP+, more information from the Microsoft.net site


Related Information:

4GuysFromRolla This is one of the best technical ASP sites out there, with Cut 'n' Paste articles clearly explaining functions that you will want to carry out on your site. I cannot emphasise how helpful this site is.
www.4guysfromrolla.com

ASP FAQS - A New site, creating alongside 4guysfromrolla. Short questions & answers
www.aspfaqs.com/aspfaqs/

www.asp101.com


© Jamie McHale 1998 - 2000 - http://www.btinternet.com/~wildfire/