ASP   «Prev  Next»

Lesson 3 ASP script syntax
ObjectiveExamine an ASP script

Classic ASP Script Syntax

As we examined the server's and browser's treatment of ASP code within an HTML page, you may have noticed some unfamiliar characters.
These delimiters[1], <% and %>, tell the interpreter where an ASP script begins and ends.
ASP, as installed, has the ability to interpret either VBScript or JScript code.
VBScript is the default, although you can direct ASP to recognize JScript code in an HTML page.
In this course we will work in VBScript, and ASP scripts will be assumed to follow VBScript syntax and coding rules.
Here is a simple example of an ASP script that stores data and then writes out that data when requested:

<HTML>
  <TITLE>This is an example</TITLE>
  <BODY>
  <%'BEGIN ASP SCRIPT
    Dim cVariable 'Initialize a character variable
    cVariable = "This is a variable in ASPScript"
    response.write cVariable
    %>
  <!–END ASP SCRIPT ––>
</BODY>
</HTML>

The HTML code that is sent to the browser will be:
<HTML>
  <TITLE>This is an example</TITLE>
  <BODY>
    This is a variable in ASPScript
  </BODY>
</HTML>

And the browser will display:

This is a variable in ASPScript

Note the use of standard HTML tags (HTML, TITLE, BODY), the ASP script bounded by its delimiters <% and %>, and the two styles of comments. The ASP interpreter will evaluate every expression between the delimiters. The client browser displays the results and messages that are generated from the ASP script.
The following example performs a calculation and displays the result by inserting a calculated value into a line of text.

<HTML>
<TITLE>Applied Discount</TITLE>
<BODY>
<%             'BEGIN ASP SCRIPT
Dim nTotal     'Initialize a numeric variable
nTotal = 20    'Set this numeric variable to a value
%>             <!–END ASP SCRIPT ––>
Your $<%=nTotal%> total with a 10% discount is $<%=nTotal – (nTotal * .10)%>!
</BODY>
</HTML>

Output Value

Placing an equals sign after the starting ASP script delimiter (<%=) is one way to direct ASP to output a value or text string. We will discuss ways to write in ASP in a later lesson.
The output from this example will be:
Your $20 total with a 10% discount is $18!

This is a simplified illustration of dynamically-generated HTML code.
Notice that we are performing a calculation in the middle of a line just before it is sent to the browser!

Local Server

You can test ASP pages on your local server by entering this as your address in your browser:
           
http://localhost/pagename.asp
where pagename is the filename you used to save the ASP page you created.
If you are using a server other than your local machine use the server's address instead (http://servername/pagename.asp).
The next lesson will explain how to create and put data into constants and variables.
[1]Delimiter: A character that separates one datum from the next.