Visual Basic Script (VBScript)
There are three ways to use VBScript in ASP pages:
1. In-line VBScript statements: The VBScript statements must be enclosed in the special in-line script tag: <% %>. The enclosed statements will be executed by the server, and the output of response.write() calls will be merged in-line with the surrounding static HTML text. In-line VBScript statements require a language declaration statement to define explicityly VBScript as the server side scripting language, if you are not sure what is the default language defined on the Web server. <%@ language="vbscript"%>
...
<%
vbscript_statement
vbscript_statement
...
%>
2. In-line VBScript expression: The VBScript expression must be enclosed in the special in-line script tag: <% %> with a leading "=" sign. The enclosed expression will be evaluated, and the resulting value will be converted into a string and merged in-line withh the surrounding static HTML text. In-line VBScript expression also require a language declaration statement to define explicityly VBScript as the server side scripting language, if you are not sure what is the default language defined on the Web server. <%@ language="vbscript"%>
...
<%
= vbscript_expresion
%>
3. Script block of VBScript statements: The VBScript statements must be enclosed by the starting script block tag . Two attributes are required on the script tag: "language" and "runat".
Please note that the script block VBScript statements will be executed by the server. But the output of response.write() calls will not be merged in-line with the surrounding static HTML text. It will be:
Inserted at the beginning of the HTTP reponse before any static HTML text, if the server's default scripting language is not VBScript.
Appended at the end of the HTTP reponse after any static HTML text, if the server's default scripting language is VBScript.
This rule was a big supprise to me. I had to post a message to microsoft.public.inetsdk.programming.scripting.vbscript, and got confirmed from Microsoft. See the article Using VBScript and JScript on a Web Page.
Mixing VBScript Statements with Static HTML Text
Here is an ASP page example to show you how in-line statements and expressions can be mixed with static HTML text: <%@ language="vbscript"%>
Good
<% if 0 <= hour(time) and hour(time) <>
moring,
<% elseif 12 <= hour(time) and hour(time) <>
afternoon,
<% else
response.write("evening,")
end if %>
Herong.
<% response.write("The current server time is: ") %>
<% = Time %> on <% = Date %>
Output: Good evening, Herong.
The current server time is: 10:01:56 PM on 5/24/2003
In the previous example, I was able to break the "if" statement into two parts and use in-line script for one part and static text for the other.
In the next example, I will show you the difference between in-line scripts and script blocks:
Text line 2.
Text line 4.
Output: Text line 2.
Text line 4.
Text line 1.
Text line 3.
Supprised? I was supprised before I learned that execution rule about script blocks mentioned in the previous section. The output also tell us that the default language on IIS 5.0 is set to VBScript. Otherwise, text line 1 and 3 would be displayed first. The problem can be avoided by using the in-line scripts: <%@ language="vbscript"%>
<%
response.write("Text line 1.
")
%>
Text line 2.
<%
response.write("Text line 3.
")
%>
Text line 4.
Now you know the different ways to use VBScript in ASP pages. But which way is better?
In-line script, <%...%>, is very flexible in mixing dynamic data with static HTML text. But the syntax is not so XML friendly.
Script block, , is not useful at all if you want to keep static HTML text in your page. It can only be used, if you want to write the entire page in a single block, and output all HTML text with response.write() calls.
Personally, I prefer script block syntax, because I could easily manipulate it with XML tools. So I will be using script blocks most of the time for my sample ASP pages in this book. Note that even HTML comment tag must be moved into the script blocks.
Variables and Expressions in VBScript
Special rules about variables and expressions:
Variables used without declarations are called "variant" variables.
Variant variables can be used to store different data types.
Variable names are case insensitive.
"=" is the assignment operator. But it is also the boolean equality operator.
"&" is the string concatenation operator.
Here is a sample ASP page, variable.asp:
Output: Tests on variables:
Sum = 6
Is the answer correct? True
"i = i=6" is a very unusual statement. First of all, variable "i" has changed its data type from integer to boolean. Secondly, two "=" signs are used next to each other, the first one is an assignment operation, and the second one is a boolean equality operator.
Arrays
Array is a build-in data structure in Visual Basic language. It can used to store a collection of data elements with indexes to access each element. Arrays can be declared to hold a fixed number of elements as: dim weekdays(5)
Arrays can also be declared to a variable number of elements as: dim weekdays()
redim weekdays(5)
other statements
redim preserve weekdays(7)
Elements in a array can be iterated with the "for each ... next" statement as: dim weekdays(5)
other statements
for each day in weekdays
other statements
next
Here is a simple ASP page to how the features of arrays:
Output Tests on arrays:
Element 1 = Mon
Element 2 = Tue
Element 3 = Wed
Element 4 = Thu
Element 5 = Fri
Two days added:
Element 1 = Mon
Element 2 = Tue
Element 3 = Wed
Element 4 = Thu
Element 5 = Fri
Element 6 = Sat
Element 7 = Sun
Element 8 =
Note that:
Indexes of elements in arrays starts with 0.
"redim" can only be used against arrays that are declared with dynamic size.
"redim" can be used repeatedly.
"redim preserve" can be used to preserve the elements that already exist.
"for each ... next" statement revealed an extra element in the array. I am not sure why?
"Collection" Class
Collection: A class representing a collection of ordered elements. The stored elements can be referred by an index number or by the key associated with the elements. It offers the following methods and properties based on the documentation of Visual Basic .NET:
"Item(ikey)": Property to return the element of the specified index or key. Item() is the default method of a collection object, so you can call this method without the method name, like object(ikey), instead of object.Item(ikey).
"Count": Property to return the number of elements in the collection.
"Add(element)": Method to add an element at the end of the collection without a key.
"Add(element, key)": Method to add an element at the end of the collection with a key.
"Remove(ikey)": Method to remove the element of the specified index or key.
Unfortunately, I was not able to write an ASP page with a new collection object.
write200x90("ASP");
No comments:
Post a Comment