VBScript must enclosed by <% and %> VBScript code block

Download Report

Transcript VBScript must enclosed by <% and %> VBScript code block

Basic Scripting in VBScript
VBScript must enclosed by <% and %>
No HTML code is allowed inside a
VBScript code block
Nested scripting block is not allowed
Spaces within a scripting block are
ignored
A VBScript code can be placed in any where
in an ASP
VB style comments are permitted
<% ‘This is to show how to write a comment %>
VBScript Variables
VBScript variables follows the same naming rules
as other programming language (e.g, C++)
Begin with an alphabetic character
Case insensitive
Variables are not typed (variants)
The type of a variable depends on its value
All types are treated the same
myVariable = 12.34
myVariable = “Hello World”
myVariable = True
myVariable = False
Types Supported inVBScript
Numeric types
Integer, byte, long, single, double
Strings, Date, Boolean (true or false)
TypeName() function
Returns the type name of a variant
myVariable = “Hello World”
IF typeName(myVariable) = “String” THEN …
Type constants
Compare the type of a variable
myVariable = “Hello World”
IF myVariable = VbString THEN …
Variable Scopes
Local variables
Variables declared inside a function or
procedure
Global variables
Variables declared outside of
functions/procedures
The scope of a global variable is within the
page
Dim var1, var2
var1 = 123 ‘global variable
Sub myProcedure()
var1 = “Hello World” ‘local variable
Arrays
Array size must been declared
Redim can be used to change array size
Array index starts from 0
Dim arrayA(29) ‘an array with 30 elements
FOR i = 0 TO 29
arrayA(i) = “testing”
NEXT
VBScript supports multi-dimension arrays
Dim arrayB(4, 4) ‘an two dimensional array
arrayB(0, 0) = “hello”
ASP Object Model
Standard objects:
Application object - application session
management
Request object - retrieve client data
Response object - send output to browsers
Session object - client session management
Standard components:
Ad Rotator - automatic rotating AD’s
Page Counter component
Database access component
File access component
Request Object
QueryString Object
Retrieve data submitted through
URL parameters
http://www.dit.ie/cgibin/listClass.asp?classID=dt2663
Form fields submitted using the “GET” method
<form action=“listClass.asp” method=“get”>
QueryString object usage
request.queryString(“classID”) returns “dt2663”
request.queryString returns a collection of namevalue
pairs
<% for each qsName in request.queryString
response.write(qsName)
next
%>
QueryString Element Count
Request.queryString(a-name).Count
Returns the number of values of a name
itemNo=JK100,JK231,JK681
request.queryString(itemNo).count ---> 3
Used for multi-valued name value pairs
Multiple checkboxes
Multiple selected items in a dropdown
list
Any form fields with multiple values
QueryString Example
<% for each qsName in request.queryString
valNum = request.queryString(qsName).count
response.write(qsName & "=")
if valNum = 1 then
val = request.queryString(qsName)
response.write(val)
else
for i = 1 to valNum
val = request.queryString(qsName)(i)
response.write(val & ",")
next
end if
response.write("<br>")
next %>
Form Object
Allows to access form submission data
The methods are similar to that of queryString
<% for each formField in request.form
valNum = request. form(formField).count
response.write(formField & "=")
if valNum = 1 then
val = request. form(formField)
response.write(val)
else
for i = 1 to valNum
val = request. form(formField)(i)
response.write(val & ",")
next
end if
response.write("<br>")
Next %>
Response Object
Generate HTML output for web browsers
Response.write() is used in a scripting block to
produce output
response.write(request.queryString(qsName))
<%= expression %> is a short cut for
request.write() function
<%= request.queryString(qsName) %>
<%
myVariable = “Hello”
youVariable = “World”
response.write(myVariable & “ “ & youVariable)
%>
<%= “Hello” & “ “ & “World” %>
Response Object Properties/Methods
Response.buffer
Set it on (true) or off (false)
response.flush() - sends bufferred output &
continue script processing
response.clear() - erase bufferred contents
response.end() - sends buffered output & stop
script processing
Response.expires = minutes
When cached pages should expire in web browsers
Response.expiresAbsoluate = date/time
When cached pages expire in web browsers
Response.redirection = URL
Branch off to the specified web page
Summary
ASP Architecture
ASP object model
Basic VBScript scripting techniques
VBScript control structures
Request object
Response object