We have seen previously the ASP model and how to install the Personal Web Server (PWS) to develop and test ASP scripts. You can also see a tutorial from Microsoft on ASP. We also reviewed Visual Basic as related to Web Programming. In this tutorial we will see how to use VB Script -- a subset of Visual Basic -- to write simple ASP scripts.
The basic reference to VB Script is the Microsoft VB Script page. To setup VB Script in your machine download its latest version from Microsoft, selecting your version of Windows and language. For example for English, Windows 98, ME & NT 4 download scr56en.exe, and for Windows 2000 is scripten.exe. You should also download a copy of the VB Script documentation to your machine.
<%@ LANGUAGE = "VBScript"%>
Active Server Pages recognize script commands entered between the pair <% %> . In this format it handles code, but not necessarily it will produce output, you need to explicitly use output commands in order to produce output. When you use the pair <%= %> any variable, function will produce automatically output. Here are two simple examples displaying Hello! in the browser:
<% Response.Write("Hello!") %> ----> explicit output
<%= "Hello!" %> ----> implicit output
We will see and discuss four examples in order to understand how VBScript is used within the ASP tags.
<%@ LANGUAGE="VBSCRIPT"%>
<body bgcolor="White">
</body>
<html>
<head>
<title>Hello World</title>
</head>
<div align="center"><h1>Hello World</h1></div>
<br><p><hr><p>
<%Response.Write("Hello World!")%>
</html>
<html>
<body bgcolor="White">
</body>
<head>
<title>Form with ASP</title>
</head>
<div align="center"><h1>Forms with ASP</h1>
<form method="POST" action="http://L500/decode.asp">
Name: <input type="text" name="name" size="60" maxlength="80"><br>
<input type="submit" value="Submit"><input type="Reset" value="Reset"></div>
</form>
</html>
<%@ LANGUAGE="VBSCRIPT"%>
<div align="center"><%= Hello %>! Today is <%= Date %> and the day and time are <%Response.Write Now%></div>
<HTML>
<HEAD>
<TITLE>Date and time ASP Page</TITLE>
</HEAD>
<BODY>
<% Private Function Hello()
Dim timNow
timNow = Time
If timNow <= CDate("12:00:00") Then
Hello = "Good morning"
ElseIf timNow <= CDate("18:00:00") Then
Hello = "Good afternoon"
Else
Hello = "Good evening"
End If
End Function%>
</BODY>
</HTML>
The line <div align="center"><%= Hello %>! Today is <%= Date %> and the day and time are <%Response.Write Now%></div> does two interesting things:
<%@ LANGUAGE="VBSCRIPT"%>
Response.Write (sFahrenheit & " degrees Fahrenheit are " & sCelsius & " degrees Celsius")
<html>
<head>
<title>convert to Celsius</title>
</head>
<body bgcolor="White">
<div align="center"><h1>Convert to Celsius</h1></div>
<br><hr></p><p>
<% Dim sFahrenheit, sCelsius, strMethod
strMethod = Request.ServerVariables("REQUEST_METHOD")
If strMethod = "GET" Then
Response.Write "<html>"
Response.Write "<head>"
Response.Write "<title>Input of convert</title>"
Response.Write "</head>"
Response.Write "<body bgcolor=White>"
Response.Write "<form action=convcelsius.asp method=Post>"
Response.Write "<p>Please enter the degrees in Fahrenheit: <input type=text name=txtFahrenheit size=6 maxlength=6></p>"
Response.Write "<input type=submit value=Submit><input type=Reset>"
Response.Write "</form></div>"
Response.Write "</body>"
Response.Write "</html>"
Else
sFahrenheit = CSng(Request.Form("txtFahrenheit"))
sCelsius = round((5 / 9 * (sFahrenheit - 32)),1)
End If %>
</BODY>
</HTML>
You can see other examples of VB Script used with ASP at the Microsoft site, or other sites on the Web, like this or this other one.
This page is maintained by Al Bento who can be reached at abento@ubalt.edu. This page was last updated on November 26, 2004. Although we will attempt to keep this information accurate, we can not guarantee the accuracy of the information provided.