115
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 Poonawala Applied Computer Science Illinois State University

Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Embed Size (px)

Citation preview

Page 1: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 2: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 3: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 4: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 5: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

5

What You Should Already Know

WWW HTML

– Basics– Forms

Page 6: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

6

HTML Forms - Review

http://www.google.com

Page 7: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

7

Query String

Page 8: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

8

Query String

Page 9: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

9

Query String

http://www.google.com/search?q=ASP

URL Field Name

Page 10: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 11: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 12: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 13: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 14: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 15: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 16: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 17: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 18: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 19: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 20: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 21: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 22: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

22

HTTP – Client/Server Computing

Request

Response

Animation

Page 23: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

23

Client-side Processing

User’s Input Server Processing

Client Server

VBScript, JavaScriptProcessing

Page 24: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

24

Server-side Processing

User’s Input

Server Processin

gASP

Client Server

Page 25: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 26: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 27: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 28: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 29: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 30: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 31: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 32: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

32

ASP Script Delimiters

<% ….ASP code here… %> Example.

<body>

<% Response.write(“Hello World”) %>

</body>

ASP delimiters

Page 33: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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").

Page 34: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 35: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

35

First.asp

<html><body>

<%response.write("Hello World!")%>

</body></html>

Exercise 1

Page 36: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 37: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 38: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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?

Page 39: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 40: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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>

Page 41: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 42: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.)

Page 43: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 44: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 45: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 46: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 47: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 48: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 49: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 50: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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”

Page 51: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

51

The following table shows subtypes of data that a Variant can contain.

Page 52: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 53: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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”

Page 54: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

54

Declaring & Using Variables <html>

<body>

<%Dim namename="Joaquin Vila"

response.write("My name is: " & name)%>

</body></html>

Page 55: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 56: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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”

Page 57: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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)

Page 58: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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)

Page 59: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 60: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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)

Page 61: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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>

Page 62: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 63: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 64: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 65: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 66: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

66

Arithmetic Operators

Assignment Operator (=) Addition ( + ) Subtraction ( - ) Multiplication ( * ) Division ( / ) Negation ( -) Exponentiation ( ^ ) Modulus ( MOD or \ )

Page 67: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

67

Operators

Assignment Operators = Examples:

Pi = 3.14159

Name = “Joaquin Vila”

A = ( B ^ 2 + C ^ 2 ) ^ 0.5

Page 68: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

68

Comparison Operators

Less Than ( < ) Less Than or Equal to (<=) Greater Than ( > ) Greater Than or Equal to (>=) Equality ( = ) Inequality ( < >)

Page 69: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

69

Logical Operators

AND OR NOT

Page 70: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 71: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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%>

Page 72: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

72

If….Then…Else (Cont’d) Example

<%

If numberValue < 30 Then

Response.write (“value is less than 30”)

End if

%>

Page 73: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

%>

Page 74: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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%>

Page 75: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

75

If…elseif…end if

<%If condition action elseif condition action elseif condtion action else actionEnd If%>

Exercise 3

Page 76: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

%>

Page 77: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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%>

Page 78: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 79: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

79

For…Next

<%

For varCount = 1 to 5

Response.write “this is loop” & varCount

Next

%>

Exercise 4

Page 80: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

80

For…step…Next

<%

For i = 2 to 10 step 2

Response.write ( i )

Next

%>

Page 81: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

81

For…step…Next

<%

For i = 10 to 1 step -1

Response.write ( i )

Next

%>

Page 82: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 83: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 84: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

%>

Page 85: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

85

Do…While…Loop

<%varCount = 0

Do While varCount < 10

Response.write (“count is ” & varCount)

varCount = varCount + 1

Loop

%>

Page 86: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

86

Do…Loop…While

Do

… code…

Loop While condition

Page 87: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 88: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 89: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

89

VBScript Procedures

There are two kinds of procedures– Sub procedure – Function procedure.

Page 90: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 91: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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 ( )

%>

Page 92: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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" )

%>

Page 93: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 94: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 95: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 96: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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 - ->

Page 97: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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))

%>

Page 98: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 99: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 100: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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".

Page 101: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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)

Page 102: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 103: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 104: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 105: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 106: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 107: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

107

Die Example

<%

Dim MyValue

Randomize

MyValue = Int((6 * Rnd) + 1)

Response.write (MyValue)

%>

Exercise 9, 10

Page 108: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 109: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 110: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 111: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 112: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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.

Page 113: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

Copyright 2001 © Vila & Poonawala

113

Date and Time Constants

Page 114: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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

Page 115: Copyright 2001 © Vila & Poonawala1 Programming ASP for the Web Presented by Dr. Joaquin Vila and Mr. Fayyaz Poonawala Developed by Dr. Joaquin Vila & Fayyaz

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