Okay, now let's try a little code. The first thing you need to do when writing anything for ASP is let it know what language you're using. If you're using VBScript, technically you can skip this and ASP will use VBScript by default, but declaring your scripting language is a good habit to get into. So the first line of your ASP code will be:
<% @ Language = "VBScript" %>
Note the <% and %>. Anything between those marks, anywhere on an .asp page, ASP regards as gist for its mill.
For our first script, let's use ASP to tell the user what date it is on the server. It's pretty straightforward:
<%@ Language = "VBScript"%>
<HTML>
<HEAD><TITLE>First Script</TITLE></HEAD>
<BODY>
<% Response.Write "<P>Today is " & Date () & ".</P>" %>
</BODY>
</HTML>
This would appear on your browser as:
Today is 3/17/99.
Here's what you should latch onto in the above example: First, the use of Response.Write.
Response.Write tells ASP to insert whatever follows into the HTML stream that's being sent to the server. Quotes delimit what ASP will send to HTML straight out. The &'s mark off any variables, functions, or subroutines, inserting the values or results directly into the HTML stream. For example, if you were to view the source for the above page on your browser, you wouldn't see the ASP code, but the following ASP-rendered HTML:
<HTML>
<HEAD>First Script</HEAD>
<BODY>
<P>Today is 4/17/99.</P>
</BODY>
</HTML>
There's also shortcut for Response.Write, the delimiters (<%= %>). The above code could be written as follows:
<P>Today is <%=Date ()%>.</P>
It works out exactly the same way. ASP code doesn't have to all be in one chunk on your page, either. It can be interpersed throughout your HTML if you wish. Suppose you wanted to display different images on a page depending on a user's color preference. (We'll go into how to grab values from forms in a little while.) You could write:
<% If varPreference = "yellow" Then %>
<IMG SRC = "/old?u=http%3A%2F%2Fdevshed.com%2FServer_Side%2FASP%2FIntroduction%2Fyellowhat.gif&y=1999">
<%Elseif varPreference = "red" Then %>
<IMG SRC = "/old?u=http%3A%2F%2Fdevshed.com%2FServer_Side%2FASP%2FIntroduction%2Fredhat.gif&y=1999">
<% End If %>
For the record, that could also be written:
<% Response.Write "<IMG SRC = "
If varPreferences = "yellow" Then
Response.Write "yellow.gif>"
Elseif varPreferences = "red" Then
Response.Write "red.gif>"
End If.%>
You can also include subroutines on your ASP page, like the following example, which produces a list of files for a given directory on the server. We'll use this code more in the next section. For now, just note the way that subroutines can be called from within the ASP page.
<% Sub GetFolderFiles (folderspec)
Set objFolder = FileObject.GetFolder(folderspec)
Set objList = objFolder.Files
For each File in objList
Response.Write File.Name
Next
End Sub
FolderName = "./text/"
Set FileObject = Server.CreateObject("Scripting.FileSystemObject")
Set Folder = FileObject.GetFolder(Server.MapPath(FolderName))
GetFolderFiles (Folder) %>
Or, if you have a subroutine that will be used on several pages (or just want to centralize your code) you can use the #includestatement, which lets you insert the contents of a file into your ASP page. The following syntax is used:
<! -- include virtual="/scripts/mycode.inc" -- !>
In the statement, virtual refers to the type of path (virtual for a virtual path, file for a local path), and "/scripts/mycode.inc" is the file to be included (included files can have any extension, but whatever you use, stay consistent to keep things straight).
On a final note, there's nothing to say that you have to use ASP to write HTML. It could just as easily be used to construct XML or JScript code as well. The potential for a completely customizeable web experience is pretty exciting.