Upload
jeffrey-barnett
View
216
Download
1
Embed Size (px)
Citation preview
Copyright 2001 © Vila & Poonawala
1
Programming ASP for the Web
Presented by
Dr. Joaquin Vila and Mr. Fayyaz Poonawala
Developed by
Dr. Joaquin Vila & Fayyaz PoonawalaApplied Computer Science
Illinois State University
Copyright 2001 © Vila & Poonawala
2
Who Is in Attendance?
Tell us about you!– Name– Company– Experience with a browser (if any)– Experience with HTML coding (if any)– Experience with Programming (if any)– Experience with ASP (if any)– Project(s) you intend to start/continue with the
materials learned from this class
Copyright 2001 © Vila & Poonawala
3
Objectives
The Objectives are to:– Compare Client-side and Server-side scripting– Provide participants with a solid introduction to
creating dynamic web pages with ASP– Discover the language features of ASP– Create Database-driven Web-sites– Learn by example
Copyright 2001 © Vila & Poonawala
4
Agenda
Introduction to Scripting Basic language features of ASP
– Variables, expressions, operators, statements ASP Object Model
– Objects, properties, methods Built-in objects Integrating Databases with ASP Examples and Miscellany
Copyright 2001 © Vila & Poonawala
5
What You Should Already Know
WWW HTML
– Basics– Forms
Copyright 2001 © Vila & Poonawala
6
HTML Forms - Review
http://www.google.com
Copyright 2001 © Vila & Poonawala
7
Query String
Copyright 2001 © Vila & Poonawala
8
Query String
Copyright 2001 © Vila & Poonawala
9
Query String
http://www.google.com/search?q=ASP
URL Field Name
Copyright 2001 © Vila & Poonawala
10
HTML Exercise
Write HTML code to create the following form. The purpose of the form is to search keywords in selected search engines
Copyright 2001 © Vila & Poonawala
11
What is ASP?
ASP stands for Active Server Pages – Created by Microsoft– Technology to build dynamic and interactive
web-sites– Uses Server-side scripting instead of Client-
side scripting– Uses VBScript or JavaScript as the
programming language
Copyright 2001 © Vila & Poonawala
12
What is ASP?
ASP is a program that runs inside IIS – IIS stands for Internet Information Services – IIS comes as a free component with Windows
2000 – IIS is also a part of the Windows NT 4.0
Option Pack • The Option Pack can be downloaded from
Microsoft
PWS (Personal Web Server) is a smaller - but fully functional - version of IIS
Copyright 2001 © Vila & Poonawala
13
Microsoft IIS
IIS includes a set of programs for building and administering Web sites, a search engine, and support for writing Web-based applications that access databases.
IIS is tightly integrated with the Windows NT and 2000 Servers in a number of ways, resulting in faster Web page serving.
Copyright 2001 © Vila & Poonawala
14
Why ASP?
Easy to learn as compared to C++, Java ASP shields the developer from complexity
and low-level tasks Leverages Microsoft technologies like
ODBC, IIS, Access and SQL Server very well
ASP pages can be viewed by IE and NetScape without modifications
Copyright 2001 © Vila & Poonawala
15
What can ASP do for you? Dynamically update content of a Web page Access databases and return the results to a browser Customize a Web page to make it more useful for
individual users The advantages of using ASP instead of CGI and
Perl, are those of simplicity and speed Provides security since your ASP code can not be
viewed from the browser Since ASP files are returned as plain HTML, they
can be viewed in any browser
Copyright 2001 © Vila & Poonawala
16
ASP Compatibility
ASP is a Microsoft Technology ChiliASP is a technology that runs ASP
without Windows OS InstantASP is another technology that runs
ASP without Windows
Copyright 2001 © Vila & Poonawala
17
ChiliASP
http://www.chilisoft.com/ Sun Chili!Soft ASP for:
– Solaris Servers – Linux Servers – AIX Servers – HP-UX Servers – Windows Servers
Copyright 2001 © Vila & Poonawala
18
TCP/IP TCP/IP
– Transmission Control Protocol/Internet Protocol is the basic communication language or protocol of the Internet.
– TCP/IP is a two-layer program. The higher layer, Transmission Control Protocol, manages the assembling of a message or file into smaller packets that are transmitted over the Internet and received by a TCP layer that reassembles the packets into the original message. The lower layer, Internet Protocol, handles the address part of each packet so that it gets to the right destination.
Copyright 2001 © Vila & Poonawala
19
HTTP
HTTP Protocol– Hypertext Transfer Protocol is an application
level TCP/IP protocol.– An application protocol is one that travels on
top of another protocol.
Copyright 2001 © Vila & Poonawala
20
HTTP
HyperText Transfer Protocol (HTTP), the underlying protocol used by the World Wide Web.
HTTP defines how messages are formatted and transmitted, and what actions Web servers and browsers should take in response to various commands.
Copyright 2001 © Vila & Poonawala
21
HTTP Basics
There are four basic states that make up a single HTTP transaction:– The connection– The request– The response– The disconnection
Copyright 2001 © Vila & Poonawala
22
HTTP – Client/Server Computing
Request
Response
Animation
Copyright 2001 © Vila & Poonawala
23
Client-side Processing
User’s Input Server Processing
Client Server
VBScript, JavaScriptProcessing
Copyright 2001 © Vila & Poonawala
24
Server-side Processing
User’s Input
Server Processin
gASP
Client Server
Copyright 2001 © Vila & Poonawala
25
Active Server Pages (ASP)
Microsoft® Active Server Pages (ASP) is a server-side scripting technology that can be used to create dynamic and interactive Web applications.
An ASP page is an HTML page that contains server-side scripts that are processed by the Web server before being sent to the user's browser.
Copyright 2001 © Vila & Poonawala
26
How does ASP Work?
When a browser (client) requests an HTML file, the server returns the file
When a browser requests an ASP file, IIS passes the request to the ASP engine
The ASP engine reads the ASP file and executes the scripts in the file
Finally, the ASP file is returned to the browser as plain HTML
Copyright 2001 © Vila & Poonawala
27
.ASP extension
Using an ASP extension in a web page indicates to the web server that there are scripts in the page.
Using an ASP extension hides the ASP scripts. – Because ASP scripts are executed on the
server, you can not view ASP code in a browser, you will only see the output from ASP which is plain HTML.
Copyright 2001 © Vila & Poonawala
28
.TXT or .SCR File Extension
If you save your ASP code in a file with extension .TXT (or .SCR) and requested it from the web server, you’ll see all of the code inside.
Copyright 2001 © Vila & Poonawala
29
More ASP Basics
ASP files are just HTML files with scripting embedded within them.
When a browser requests an ASP file from the server, it is passed on to the ASP processing DLL for execution.
Any scripting commands embedded from the original HTML file are executed and then removed from the results.
Copyright 2001 © Vila & Poonawala
30
ASP Syntax Rule An ASP file usually contains HTML tags, just as an
ordinary HTML file. An ASP file can contain server scripts, surrounded by
the delimiters <% and %>.
Server scripts are executed on the server, and can contain any – expressions, – statements, or– procedures,
that are valid for the scripting language you choose to use.
Copyright 2001 © Vila & Poonawala
31
ASP Tags
To distinguish the ASP code from the HTML inside your files, ASP is placed between <% and %> tags.
The tag combination implies to the ASP processor that the code within should be executed by the server and removed from the results.
Copyright 2001 © Vila & Poonawala
32
ASP Script Delimiters
<% ….ASP code here… %> Example.
<body>
<% Response.write(“Hello World”) %>
</body>
ASP delimiters
Copyright 2001 © Vila & Poonawala
33
Response Object
The Write method of the ASP Response Object is used to send content to the browser.
Example: the following ASP statement sends the text "Hello World" to the browser:
Response.Write("Hello World").
Copyright 2001 © Vila & Poonawala
34
Write and Run your First ASP “The best way to learn about ASP pages is to write
your own.” Microsoft©
To create your first ASP page, use a text editor.– NotePad (any text editor will do)– MS Visual Interdev– ASP Edit 2000– ASP Express (http://www.aspexpress.com)
Use the editor to insert script commands into an HTML page.
Save the page with an .asp file name extension
Copyright 2001 © Vila & Poonawala
35
First.asp
<html><body>
<%response.write("Hello World!")%>
</body></html>
Exercise 1
Copyright 2001 © Vila & Poonawala
36
Request Object
The Request object retrieves the values that the client browser passed to the server during an HTTP request.
Copyright 2001 © Vila & Poonawala
37
Interacting with Forms
One way to retrieve information from a form is by using – Request.QueryString command
• Used with forms that use the GET method
– Request.Form command• Used with forms that use the POST method
Copyright 2001 © Vila & Poonawala
38
Example of a Simple Interaction
<form action=hello.asp method=get>
<input type=text name = who >
<input type=submit>
<form>
<%
Response.Write ("Hello " & Request.QueryString("who") & "!")
%>
<BR>How are you today?
Copyright 2001 © Vila & Poonawala
39
Including HTML Tags
<html><body>
<%response.write(" <h2>Hello World! <br>You could include HTML tags to format the text! </h2> ")%>
</body></html>
See Example 2 Exercise 2
Copyright 2001 © Vila & Poonawala
40
<SCRIPT> Blocks
You may also place your ASP code between <SCRIPT> </SCRIPT> blocks. – Usually <script> blocks are executed at the client as
normal client-side scripts.
– To direct your script block to execute on the server, use the RUNAT command within your <script) as follows:
<SCRIPT Language=“VBScript” RUNAT=“Server”>
---- your code ----
</SCRIPT>
Copyright 2001 © Vila & Poonawala
41
Changing the Scripting Language
You may change the default (VBScript) scripting language by placing the following tag at the beginning of your web page:
<%@ LANGUAGE = ScriptingLanguage%>
ScriptingLanguage can be any language for which you have the scripting engine installed.
Copyright 2001 © Vila & Poonawala
42
Scripting Languages
ASP comes with Jscript, as well as VBScript.– Jscript - Microsoft's implementation of Javascript.
Other scripting languages, – PERL,
– REXX, or
– Python
(You will have to install scripting engines for them.)
Copyright 2001 © Vila & Poonawala
43
VBScript: The Default ASP Scripting Language
A scripting language is a simple programming language.
VBScript is a light version of the Microsoft Visual Basic programming language
Copyright 2001 © Vila & Poonawala
44
Variables
A named storage location that can contain data that can be modified during program execution. – Examples:
strName = “Joaquin Vila”
Day = 21
Copyright 2001 © Vila & Poonawala
45
Rules for Variable Names The length of variable name should not exceed 255
characters. A variable name must begin with a letter. A variable can not contain periods ( . )
Examples:– Valid variable names :
numOfDayISU white
– Invalid variable names:.fact3cool
Copyright 2001 © Vila & Poonawala
46
Option Explicit
Use the OPTION EXPLICIT statement to ensure that all variables are declare properly (explicitly) using the DIM statement.
Copyright 2001 © Vila & Poonawala
47
Scope of Variables Global Variables
– A variable declared outside a procedure can be changed by any script in the ASP file in which it is declared.
Local Variables– A variable declared inside a procedure is created and
destroyed every time the procedure is executed. – No ASP scripts outside that specific procedure can
access that variable. You could also make a variable accessible to
several ASP pages by declaring it either as a session variable or as an application variable.
Copyright 2001 © Vila & Poonawala
48
Session & Application Variables
Session variables– store information about one single user.– available to all pages in one application.
Application variables– store information about all users in a specific
application.– available to all pages in one application.
Copyright 2001 © Vila & Poonawala
49
Declaring & Initializing Variables
Variable Declaration.Dim something
VBScript is a loosely typed language.something = 33
something = “Hello World”
something = 33 & “Hello World”
something = “Hello World” & 33
Copyright 2001 © Vila & Poonawala
50
Data Types in VBScript
Only one data type : Variant This data type can store numeric and
character data. E.g.
numOfDays = 30
numOfDays = “thirty”
Copyright 2001 © Vila & Poonawala
51
The following table shows subtypes of data that a Variant can contain.
Copyright 2001 © Vila & Poonawala
52
Boolean (True / False)
True– The True keyword has a value equal to -1.
False– The False keyword has a value equal to 0.
Copyright 2001 © Vila & Poonawala
53
String Concatenation
Concatenate (join 2 strings to form 1) ‘ &’ operator is used to concatenate strings Example:
strMessage1 = “Hello”
strMessage2 = “world”
strMessage = strMessage1 & strMessage2
Result : “Helloworld”
Copyright 2001 © Vila & Poonawala
54
Declaring & Using Variables <html>
<body>
<%Dim namename="Joaquin Vila"
response.write("My name is: " & name)%>
</body></html>
Copyright 2001 © Vila & Poonawala
55
Type Conversions VBScript handles data type conversion
automatically.
CDbl( ) converts to Double
Cint( ) converts to Integer
CStr( ) converts to String
Cdate( ) converts to Date
Clng( ) converts to Long
Copyright 2001 © Vila & Poonawala
56
Constants
Constants are like variables except that once they are assigned a value, their value cannot change
The ‘Const’ keyword is used to define a constant
By convention, constant names are written in Upper Case
Example: Const HELLO = “hello world”
Copyright 2001 © Vila & Poonawala
57
Arrays An array is an ordered set of values
associated with a single variable name.– Syntax:
Dim ArrayName (MaximumIndex) To access the elements of an array
arrayName (elementIndex) The first element of an array has an index of
zero (0)
Copyright 2001 © Vila & Poonawala
58
DIM Temperature (4)
7.5
65.0
72.1
87.5
12.3Temperature (0)
Temperature (1)
Temperature (2)
Temperature (3)
Temperature (4)
Copyright 2001 © Vila & Poonawala
59
DIM Temperature (4)
7.5
65.0
72.1
87.5
12.3Temperature (0)
Temperature (1)
Temperature (2)
Temperature (3)
Temperature (4)
Elements
Copyright 2001 © Vila & Poonawala
60
DIM Temperature (4)
SubscriptorIndex
7.5
65.0
72.1
87.5
12.3Temperature (0)
Temperature (1)
Temperature (2)
Temperature (3)
Temperature (4)
Copyright 2001 © Vila & Poonawala
61
Declaring an array <html>
<body>
<%Dim myFamily(4) myFamily(0) = "Joaquin" myFamily(1) = "John" myFamily(2) = "Alfred" myFamily(3) = "Carmen" myFamily(4) = "Alfred Jr. "
For i = 0 to 4 response.write(myFamily(i) & "<br>")Next%>
</body></html>
Copyright 2001 © Vila & Poonawala
62
Array Function Array Function
– Returns a Variant containing an array.– The array function allows you to assign values to the
elements of an array in one line of code.
Dim AA = Array(10,15,20)B = A(2)
B is now 20.
Copyright 2001 © Vila & Poonawala
63
Multi-Dimensional arrays
A multidimensional array has more than one dimension/index.
The maximum number of dimensions possible in VBScript is 60.
Example:– Dim arr2D(40,90)– This declares a 2 dimensional array that has 41
rows and 91 columns
Copyright 2001 © Vila & Poonawala
64
Multidimensional Arrays
Dim Data(2, 4)
5 10 15 20 25
30 35 40 45 50
55 60 65 70 75
0 1 2 3 4
0
1
2
Rows
Columns
Data
Copyright 2001 © Vila & Poonawala
65
Multidimensional Arrays
Dim Data (2, 4)
Data(1, 2)
5 10 15 20 25
30 35 40 45 50
55 60 65 70 75
1
2
Copyright 2001 © Vila & Poonawala
66
Arithmetic Operators
Assignment Operator (=) Addition ( + ) Subtraction ( - ) Multiplication ( * ) Division ( / ) Negation ( -) Exponentiation ( ^ ) Modulus ( MOD or \ )
Copyright 2001 © Vila & Poonawala
67
Operators
Assignment Operators = Examples:
Pi = 3.14159
Name = “Joaquin Vila”
A = ( B ^ 2 + C ^ 2 ) ^ 0.5
Copyright 2001 © Vila & Poonawala
68
Comparison Operators
Less Than ( < ) Less Than or Equal to (<=) Greater Than ( > ) Greater Than or Equal to (>=) Equality ( = ) Inequality ( < >)
Copyright 2001 © Vila & Poonawala
69
Logical Operators
AND OR NOT
Copyright 2001 © Vila & Poonawala
70
Control Structures In structured programming, we use three
basic control structures:– Sequence Structure
– Selection Structures• If….Then….Else• Select….Case
– Repetition Structures• For ….. Next• Do….While
Copyright 2001 © Vila & Poonawala
71
Selection
We use selection to handle making choices A condition that is evaluated to be either
true or false determines the next action
Is a > b?
Print “Yes”Print “Yes” Print “No”Print “No”
True False
<%If a >b Then response.write ( “Yes”)Else response.write ( “No”)”End If%>
Copyright 2001 © Vila & Poonawala
72
If….Then…Else (Cont’d) Example
<%
If numberValue < 30 Then
Response.write (“value is less than 30”)
End if
%>
Copyright 2001 © Vila & Poonawala
73
If….Then…Else (Cont’d) Example 2:
<%
If numberValue < 30 Then
Response.write (“value < 30”)
Else
Response.write (“value > 30”)
End if
%>
Copyright 2001 © Vila & Poonawala
74
Example of a Simple Interaction
<form action=hello.asp method=get> <input type=text name = who > <input type=submit><form>
<%If Request.QueryString("Who")< >" " Then
Response.Write ("Hello " & Request.QueryString("who") & "!")Response.Write ("<br>How are you today?")
ElseResponse.write ("I wish I knew your name :(")
End If%>
Copyright 2001 © Vila & Poonawala
75
If…elseif…end if
<%If condition action elseif condition action elseif condtion action else actionEnd If%>
Exercise 3
Copyright 2001 © Vila & Poonawala
76
Case Statement<%
Select Case varConfirmCase “Fax”
Response.write “confirm with fax”
Case “Email”
Response.write “confirm with Email”
Case “Phone”
Response.write “confirm with Phone”
End Select
%>
Copyright 2001 © Vila & Poonawala
77
Select…Case (Cont’d)<%Select Case varMonth
Case “August” , “September”, “October” Response.write “Fall Selected”Case “April”, “May” Response.write “Spring Selected”Case Else Response.write “Invalid Selection”
End Select%>
Copyright 2001 © Vila & Poonawala
78
Repetition
Repetition (looping, iteration) performs a segment of code as long as condition remains true or until a condition is false
??
True
False
exit loop
Copyright 2001 © Vila & Poonawala
79
For…Next
<%
For varCount = 1 to 5
Response.write “this is loop” & varCount
Next
%>
Exercise 4
Copyright 2001 © Vila & Poonawala
80
For…step…Next
<%
For i = 2 to 10 step 2
Response.write ( i )
Next
%>
Copyright 2001 © Vila & Poonawala
81
For…step…Next
<%
For i = 10 to 1 step -1
Response.write ( i )
Next
%>
Copyright 2001 © Vila & Poonawala
82
For Each...Next
A For Each...Next loop repeats a block of code for each element in an array or a collection.
The difference with the For … Next loop is that you do not have to specify the number of items you want to loop through.
Copyright 2001 © Vila & Poonawala
83
For Each … Next - Example
dim family(2)family(0)=“Joaquin"family(1)=“Carmen"family(2)=“John"
For Each member in family document.write(member & "<br>")Next
Copyright 2001 © Vila & Poonawala
84
Do…While…Loop
Used to execute a loop until a certain condition is satisfied
Syntax:<%
Do While condition
…..(statements to be executed)
Loop
%>
Copyright 2001 © Vila & Poonawala
85
Do…While…Loop
<%varCount = 0
Do While varCount < 10
Response.write (“count is ” & varCount)
varCount = varCount + 1
Loop
%>
Copyright 2001 © Vila & Poonawala
86
Do…Loop…While
Do
… code…
Loop While condition
Copyright 2001 © Vila & Poonawala
87
Do…Until…Loop
If i equals 5, the code inside the loop will never be executed.
Do Until i=5 …code…Loop
Copyright 2001 © Vila & Poonawala
88
Exit a Loop
You can exit a Do...Loop statement with the Exit Do keyword.
Do Until i=5 i=i-1 If i<5 Then Exit DoLoop
Exercise 5
Copyright 2001 © Vila & Poonawala
89
VBScript Procedures
There are two kinds of procedures– Sub procedure – Function procedure.
Copyright 2001 © Vila & Poonawala
90
Sub procedure:
A sub procedure is a series of statements, enclosed by the Sub and End Sub statements.
A sub procedure allows you to write code that could be used more than once.
A sub procedure does not return a value. A sub procedure may or may not take
arguments.
Copyright 2001 © Vila & Poonawala
91
Sub Procedure - Example
<%
Sub printHeading ( )
Response.write ("<H2>Illinois State University</H2><BR>")
Response.write (date & "<BR>")
End Sub
.
.
.
Call printHeading ( )
%>
Copyright 2001 © Vila & Poonawala
92
Sub Procedure with Arguments
<%
Sub printHeading (title)
Response.write ("<H2>" & title & "</H2><BR>")
Response.write (date & "<BR>")
End Sub
…
Call printHeading ("Hello World" )
%>
Copyright 2001 © Vila & Poonawala
93
Function Procedure
Similar to Sub Procedure Functions RETURN a value (unlike Sub
Procedures) Has 4 parts:
Function function name
… action statements…
function name = some value
End Function
Copyright 2001 © Vila & Poonawala
94
Function (Cont’d)
<%
function Convert(celsius)
Convert = celsius * 9/5 + 32
end function
…
Dim celsius
celsius = 35
response.write(celsius & " Celsius <BR>")
response.write(Convert(celsius) & " Fahrenheit")
%>
Exercise 6, 7
Copyright 2001 © Vila & Poonawala
95
Exit Statement
Exit Statement– Exits a block of Do...Loop, For...Next,
Function, or Sub code.
Exit Do
Exit For
Exit Function
Exit Property
Exit Sub
Copyright 2001 © Vila & Poonawala
96
Comments
Adding a comment to HTML or ASP can help you clarify the code.– ASP Comments
• Place an apostrophe ( ‘ ) or the keyword REM at the beginning of each comment
‘ This is an ASP commentREM This is also a Comment
– HTML Comments
<!- - This is an HTML comment - ->
Copyright 2001 © Vila & Poonawala
97
Selected VBScript Functions
The ucase and lcase functions return a string that has been converted to uppercase or lowercase characters.
<%
txt="I love ASP Programming!"
response.write(ucase(txt))
response.write("<br>")
response.write(lcase(txt))
%>
Copyright 2001 © Vila & Poonawala
98
String Functions
The Trim, LTrim and RTrim functions Remove leading or trailing spaces from a given string.
<%name=" Joaquin "response.write("Hello" & trim(name) & "Vila<br>")response.write("Hello" & rtrim(name) & "Vila<br>")response.write("Hello" & ltrim(name) & "Vila<br>")%>
HelloJoaquinVilaHello JoaquinVilaHelloJoaquin Vila
Copyright 2001 © Vila & Poonawala
99
String Functions
InStr Function– Returns the position of the first occurrence of
one string within another.
Dim SearchString, SearchChar, MyPosSearchString ="XXpXXpXXPXXP" SearchChar = "P" MyPos = Instr(4, SearchString, SearchChar, 1)
A textual comparison starting at position 4. Returns 6.
Copyright 2001 © Vila & Poonawala
100
String Functions
StrReverse Function– Returns a string in which the character order of
a specified string is reversed.
Dim MyStr
MyStr = StrReverse("VBScript")
MyStr contains "tpircSBV".
Copyright 2001 © Vila & Poonawala
101
Other String Functions
Left Function– Returns a specified number of characters from
the left side of a string.Left(string, length)
Right Function– Returns a specified number of characters from
the right side of a string.Right(string, length)
Copyright 2001 © Vila & Poonawala
102
Other String Functions Mid Function
– Returns a specified number of characters from a string.Mid(string, start[, length])
Example:
<% dim wordword = "Joaquin"response.write(Mid(word, 3,1))
%>
– The result is a Exercise 8
Copyright 2001 © Vila & Poonawala
103
IsNumeric Function IsNumeric Function
– Returns a Boolean value indicating whether an expression can be evaluated as a number.
Dim MyVar, MyCheckMyVar = 53 ' Assign a value.MyCheck = IsNumeric(MyVar) ' Returns True.MyVar = "459.95" ' Assign a value.MyCheck = IsNumeric(MyVar) ' Returns True.MyVar = "45 Help" ' Assign a value.MyCheck = IsNumeric(MyVar) ' Returns False.
Copyright 2001 © Vila & Poonawala
104
Rnd Function
Rnd ( )– Returns a random number.– The Rnd function returns a value less than 1 but
greater than or equal to 0. – Before calling Rnd, use the Randomize
statement without an argument to initialize the random-number generator with a seed based on the system timer.
Copyright 2001 © Vila & Poonawala
105
Rnd Function (Cont.)
To produce random integers in a given range, use this formula:
Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
Here, upperbound is the highest number in the range, and lowerbound is the lowest number in the range.
Copyright 2001 © Vila & Poonawala
106
Randomize
Randomize (number) Statement– Initializes the random-number generator.– The number argument can be any valid numeric
expression.– Randomize uses number to initialize the Rnd
function's random-number generator, giving it a new seed value. If you omit number, the value returned by the system timer is used as the new seed value.
Copyright 2001 © Vila & Poonawala
107
Die Example
<%
Dim MyValue
Randomize
MyValue = Int((6 * Rnd) + 1)
Response.write (MyValue)
%>
Exercise 9, 10
Copyright 2001 © Vila & Poonawala
108
Eval Function
Eval– Evaluates an expression and returns the result.
<% Dim result, expression expression = "(3 + 5) * 1.5" result = eval (expression) response.write (result)%>
Exercise 11
Copyright 2001 © Vila & Poonawala
109
Date Function
Date Function– Returns the current system date.
Time Function– Returns a Variant of subtype Date indicating the
current system time.
Now Function– Returns the current date and time according to the
setting of your computer's system date and time.
Copyright 2001 © Vila & Poonawala
110
Date & Time Functions
<%response.write (date & "<BR>")
response.write (time & "<BR>")
response.write (now)
%>
8/10/20013:58:43 AM8/10/2001 3:58:43 AM
Copyright 2001 © Vila & Poonawala
111
Extra…<%= …%>
When you have a small amount of ASP code to include in an HTML document you use the
<%= … %>rather than a response.write ( )
<%=time%><%response.write(time)%>} Same
Copyright 2001 © Vila & Poonawala
112
Month Function
Month Function– Returns a whole number between 1 and 12,
inclusive, representing the month of the year.
Dim MyVar
MyVar = Month(Now)
MyVar contains the number corresponding to the current month.
Copyright 2001 © Vila & Poonawala
113
Date and Time Constants
Copyright 2001 © Vila & Poonawala
114
<%
if vbFriday = Weekday(now) then
response.write ("TGI Friday!")
else
response.write ("Have a nice day!")
end if
%>
Exercise 12
Copyright 2001 © Vila & Poonawala
115
FormatDateTime Function
<% response.write(FormatDateTime(date(),vbgeneraldate))%>
<BR>
<% response.write(FormatDateTime(date(),vblongdate))%>
<BR>
<% response.write(FormatDateTime(now(),vblongtime))%>
<BR>
<% response.write(FormatDateTime(now(),vbshorttime))%>
8/10/2001 Friday, August 10, 2001 4:23:40 AM 04:23