using data active server pages objectives in this chapter, you will: learn about variables and...

41
Using Data Using Data Active Server Pages

Upload: reynold-simpson

Post on 11-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Using DataUsing Data

Active Server Pages

Page 2: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

ObjectivesObjectives• In this chapter, you will:• Learn about variables and constants• Explore application and session variables• Learn the use of data types and variable

declarations• Learn the role that variables and constants

have in an application• Learn the benefits and types of modules• Learn how to create objects from classes• Learn about ASP’s object model

Page 3: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Variables and ConstantsVariables and Constants

•A constant is a memory location that cannot change throughout the life of a program

•A variable is a memory location that stores values that can change throughout the life of a program

•To use a variable in your code, you must give it a name

•Variable names in VBScript are not case sensitive

Page 4: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Variables and ConstantsVariables and Constants

• In VBScript, variables names must follow these rules:▫Must begin with an alphabetic character

▫Cannot contain an embedded period

▫Cannot exceed 255 characters

▫Must be unique in the scope in which they are declared

▫Cannot be a reserved word (see msdn.microsoft.com/scripting for a complete listing of reserved words

Page 5: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Variables and ConstantsVariables and Constants

•Variable names in JavaScript must also

follow some standard rules:

▫Must begin with a letter (either uppercase

or lowercase), an underscore (_), or a dollar

sign. All subsequent characters can be

letters, numbers, underscores, or dollar

signs

▫Cannot be a reserved word

Page 6: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Application and Session VariablesApplication and Session Variables• Active server pages are dynamic• When an ASP is processed, its variables are

active only while the response page is being generated by the ASP script

• You can make data available after the response pages generate; you do this by using special variables

• The Application variable can be used by everyone who accesses the application

• Session variables can only be used by the user who is logged on to the application during that session

Page 7: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Data TypesData Types

•Variables contain data; data comes in data types

•The data type indicates characteristics of the contained data

•Different programming languages allow for different data types

•VBScript allows for only one data type, called a variant

•A variant can store different types of data at different times

Page 8: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Data Types and Their Three-Data Types and Their Three-Character PrefixesCharacter Prefixes

Page 9: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

The Scope of a VariableThe Scope of a Variable

•A variable’s scope specifies when the application does and does not have access to the variable

•You indicate the scope that you want a variable to have by declaring the variable in a specific place, such as:▫Application object▫Session object▫Script▫Procedure

Page 10: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Declaring VariablesDeclaring Variables• Once you decide to use a variable or constant

and then pick its data type, the next step is to declare the variable

• A variable declaration is a line of code that tells the program what variables you will use

• In VBScript, a typical variable declaration includes the following: Dim myVar

• A typical variable declaration in JavaScript is as follows: var Myvar;

• While VBScript do not require it, you should declare all variables at the beginning of the script

Page 11: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Declaring ConstantsDeclaring Constants

•The next example shows how to declare constants in VBScript

•JavaScript does not support constants; therefore, in JavaScript, you need to use a regular variable, set to a specific value in place of a constant

Const myConst = 2

2

Page 12: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Declaring Application and Declaring Application and Session VariablesSession Variables

• You declare an Application or Session variable with an assignment statement, which allows you to give a variable a value

• VBScript and JavaScript declare variables in this manner:Application (“myVar”) = 0OrApplication (“myVar”) = “This is a string”Application (“myVar”) = 0;OrApplication (“myVar”) = “This is a string”;

2

Page 13: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Using Variables and Constants Using Variables and Constants in an Applicationin an Application

•Now that you have a good grounding in variables and constants, it’s time to take a look at them within an application

Page 14: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

The Inches to Feet ProgramThe Inches to Feet Program

2

Page 15: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

After the ConversionAfter the Conversion

2

Page 16: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Creating ModulesCreating Modules

•A module is a series of steps designed to perform a single task

•Modularization is the dividing of an application into two parts

•There are two different kinds of modules:▫Subroutines (also known as procedures)

▫Functions

•Both allow you to reuse a segment of code repeatedly in your programs

Page 17: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Subroutines or ProceduresSubroutines or Procedures

• A subroutine is a series of steps that accomplishes a task but does not usually return a value

• There are two steps involved in creating a subroutine:▫ Defining – Calling

• The following shows how to define a subroutine in VBScript:SUB subroutine_name • • • [Code goes here]END SUB

Page 18: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Subroutines or ProceduresSubroutines or Procedures

•A module in JavaScript looks a little different from VBScript:

function function_name( )

{

[CODE GOES HERE]

}

[END CODE]

Page 19: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Subroutines or ProceduresSubroutines or Procedures

•In JavaScript, a subroutine begins with

the keyword FUNCTION followed by the

name of the function

•A subroutine call in JavaScript consists

of the name of the subroutine followed

by parentheses and a semicolon

Page 20: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Creating a VBScript SubroutineCreating a VBScript Subroutine

•We are going to use a VBScript subroutine in the conversion application in this chapter

•Then, we will investigate the JavaScript version of the same code

•To modify the calc_feet.asp file in VBScript use the steps on pages 32 to 34 in the textbook

Page 21: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Calling inches_to_feetCalling inches_to_feet

Page 22: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Reviewing a JavaScript SubroutineReviewing a JavaScript Subroutine

• If you wanted to accomplish the same task with JavaScript, you would use the following code

• (Note that the bolded sections reflect the differences between the JavaScript and VBScript versions, and the “…” was used to indicate code that was unchanged and thus deleted for brevity)

•Refer to the Code Example on pages 34 and 35 in the handouts

Page 23: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

FunctionsFunctions

• A function accomplishes a series of steps and returns a single value

• The code on page 35 in the handout shows a VBScript function that adds two numbers

• The two numbers passed into the function are called arguments

• An argument is a variable used to do calculations in the function itself

• A function can have zero or more arguments, separated by commas

Page 24: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Calling a FunctionCalling a Function

•The following, written in VBScript, calls a function:

Dim result

result = Add (1, 2)

•To do the same in JavaScript, you would write the following code:

var result;

result = Add (1, 2);

Page 25: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Adding a VBScript Function to Adding a VBScript Function to calc_feet.aspcalc_feet.asp

•In the following steps, you will add a function to the calc_feet.asp page in VBScript

•This function will be responsible for converting inches into feet

•Follow the directions on pages 36 and 37 of the textbook to add a function to the calc_feet.asp page in VBScript

Page 26: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Adding a VBScript Function to Adding a VBScript Function to calc_feet.aspcalc_feet.asp

• Code Dissection

▫ The first bold line of code is the function definition: FUNCTION convert_to_feet(intinches)

▫ The first block of bold code includes the FUNCTION…END FUNCTION keywords

▫ In the first block of bold code, the line that begins with convert_to_feet=round(intinches/12,1) is the actual function

▫ The last bold line calls the function

Page 27: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Request a Function in JavaScriptRequest a Function in JavaScript

• In JavaScript, a function uses the keyword “return” to return a value from the function

• The code on page 38 in the handoutshows the JavaScript version of the previous function example

• Code Dissection

▫ The first block of bold code shows how to create a function in JavaScript

▫ The second block of code shows how to call a function in JavaScript

Page 28: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Creating Objects from ClassesCreating Objects from Classes

•A class defines the properties and methods of the objects that reside in it

•Once you create a class, you can use it to create multiple objects

•You can create classes in VBScript or in JavaScript

Page 29: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Classes in VBScriptClasses in VBScript

•Within VBScript, you declare properties and methods associated with a class inside the CLASS…END CLASS keywords

•You name a class by using the three letter prefix “cls” and the name of your choice

•To create a class in VBScript perform the instructions listed on pages 39 and 40 in the handouts

Page 30: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Classes in JavaScriptClasses in JavaScript

•To create a class in JavaScript, you need to create a function that creates the class (which is an object itself) and that declares the properties and methods to be given to any subsequent objects from that class

•The function name should be the name of the class, which is clsConversion in the example on pages 40 and 41 in the handout

Page 31: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Declaring an Object from a ClassDeclaring an Object from a Class• The next step after you create a class in

VBScript or JavaScript, is to declare an object that will be based on the class

• Declaring an object from a class is called creating an instance of the class

• Code ExampleThe following shows how to declare an object from a

VBScript class:Dim objConversionSet objConversion = new clsConversionThe following shows how to declare an object from a

JavaScript class:var objConversion;objConversion = new clsConversion( );

Page 32: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Using Classes in VBScriptUsing Classes in VBScript• The running example in the handouts converts

inches to feet• The use of classes allows the developer of the

class to hide from the programmer the details of how the conversion is done

• The use of a class makes it possible for the programmer to create an application object without actually having to learn how to do the conversion itself

• To include the clsConversion.cls class in his or her script, the programmer would use the following line:<!-- #include file=“clsConversion.cls”-->

Page 33: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Using Classes in VBScriptUsing Classes in VBScript

•The code did not specify where clsConversion.cls is located

•The class file you just created is called a Server-Side Include (SSI) file

•Sometimes for management reasons, you will want your shared script files to reside in their own directories

Page 34: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Using Classes in VBScriptUsing Classes in VBScript

•To do this, you will need to use a virtual path, which is a directory path that assumes that the Web application’s root folder is the virtual directory created for this application

•To modify the calc_fee.asp to use the clsConversion class, refer to the processes on pages 43 and 44 in the handouts

Page 35: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Using ASP’s Object ModelUsing ASP’s Object Model

•You have encountered brief mentions of the Application, Session, Request, and Response objects

•It is now time to review them in detail so that you understand properties, methods, and events that are available to you for use in future chapters

Page 36: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Application ObjectApplication Object

• The Application object, which initializes server variables that are used throughout the life of a Web application, supplies the necessary interface you need to create Web applications

Page 37: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Session ObjectSession Object

• The Session object, which initializes a user’s session inside a Web application, provides a mechanism for creating server variables and firing common events related to a user’s session

Page 38: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Request ObjectRequest Object

• The Request object supplies information regarding the request the user sends to the Web application

Page 39: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

Response ObjectResponse Object

• The Response object provides a way of sending a response message to the client in HTML

Page 40: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

SummarySummary• Data exists in a program as a variable or as a

constant

• A constant is a memory location that cannot change throughout the life of a program

• A variable is a memory location that stores values that can change throughout the life of a program

• Variables contain data; data comes in data types

• A variable declaration is a line of code that tells the program what variables you will use

Page 41: Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn

SummarySummary

• A module is a series of steps designed to perform a single task

• You can create objects efficiently by using a class

• The Application, Sessions, Request, and Response objects are common in ASP

• Each has its own unique combination of available properties, methods, and events