|
ASP is a scripting language created by Microsoft. ASP enables you to
combine HTML with powerful server side functionality. ASP is easy to use
as it is designed around the object orientated programming model. ASP
can be written in the two native scripting formats: Visual Basic and JScript (similar to JavaScript) and optional third party scripting
engines can also be installed for Perl, Rexx & Python.
Basic knowledge in any
scripting language will hold you in good stead with ASP. ASP is
supported by Microsoft's IIS web servers and third party support is
available for most Linux based servers i.e. Apache.
RULES: There are four
basic rules for incorporating ASP into your web pages.
-
Files must be named with the
.asp extension
-
The language you are using
for the ASP page must be declared on the very first line of your HTML
page.
-
The use of <%
%> delimiters are used to encapsulate any ASP code or directives.
-
The ASP page must be called
or served from a web server i.e. You can not just open the page in your
browser.
In all of these examples I
have used VBSCRIPT as the default language. The reason: I
consider VBSCRIPT to be less prone to human coding errors as less code
is required over JSCRIPT. If you do wish to use JSCRIPT then code
formatting rules are very similar to JAVASCRIPT in that the use of the
;
to end a line of code is required and other formatting rules associated
with JSCRIPT.
Example:
http://www.mywebsite.com/myaspfile.asp
<% @
LANGUAGE="VBSCRIPT" %>
' Declare the coding
language that is to be used
<html>
<head>
</head>
<body>
</body>
</html>
Comments:
Throughout these
examples you will see me use either // or ' to give explanations on what
the line of code is for. These are called comments and are not processed
by the server. When writing any code it is wise to use comments to
indicate what each particular piece of code is being used for. There is
nothing worse than having to alter code that you worked on 12 months ago
and having to spend some time trying to figure why you used this
particular line / area of code or why it was written in the that manner.
Comments can save you lots of time and frustration down the track.
Variables:
If you are already familiar with
scripting languages then you will have no problems declaring & using
variables in ASP other than the call / display procedures. If you are
new to scripting then the concept behind variables are simply that they
are used to hold information. Each variable you use is given an
identifying name then the value that the variable is to hold. You can
then access any variables value by referencing it's name.
E.g.
Dim myVariable
myVariable = "This is the value of my variable"
The word "Dim" is used in VBSCRIPT to declare or inform
the the server that a variable is being created. You do not have to
immediately give a declared variable a value. A variable's value can be
set or modified at any time throughout your code.
There are two types of variables that can be
used: Global and Local.
There are differences in their use and the way in which they will hold a
value. I will not go into detail here on their separate uses other than
to say that a Globally declared variable can be accessed at any time
throughout your code and a Local variable may only be accessed / used in
a specific area or procedure within your code. In all of these examples
unless specified we are using Global variables.
Naming Conventions:
At times pages may end up with
100's of declared variables if your application is large. To help you
remember what every variable is being used for you should name your
variables in a descriptive manner. A rule of thumb is to use an
uppercase character for the second part of your variable if required ie.
myVariable or with an underscore my_variable. Another well used
option is to keep you informed on what type of value is being stored in
your variable ( for value types see below ). If your variable is to hold
a string or text then the first part of the name can be strMyVariable
with the str informing you that the variable holds text or a string. For
integers or numbers use intMyVariable.
Example:
If you require a variable to hold a Grand
Total Amount for a number of variables that are being added together you
could use:
Dim int_grandTotal.
Whichever naming convention you decide to
use, keep it uniform across you site.
NOTE:
If copying code directly from the page and not from the text box's then
copy code and paste into notepad, copy from notepad into your HTML
editor! If you do not do this then the code may be corrupt, depending on
your HTML editor.
<%@LANGUAGE="VBSCRIPT"%>
<%
Dim variable_name
' Declare a variable with the name "variable_name"
Dim variable_name2
' Declare a variable with the name "variable_name2"
variable_name = "Vortex"
'
Assign variable_name with a value
variable_name2 = "Media"
%>
<html>
<head>
<title> My ASP Trial </title>
</head>
<body>
<p>
<font color="#800000" face="Arial" size="4">
You chose to use <%= variable_name %> <%= variable_name2 %> for your IT
resources.
</font>
</p>
</body>
</html>
You will notice that we have
encapsulated the ASP code with the <% & %> delimiters. The = part of the
delimiter informs the ASP engine that the variable's value is to be
written directly to the page.
Example:
Create a page with the code below, save & upload to your server then
call the page. |