Open Directory Site ASP Tutorials

ASP | XML | VBScript | JavaScript | ADO | CSS | XMLDOM | PHP | Operating Systems

Home >> ASP >> ASP Variables

ASP Variables

 

Lifetime of Variables

A variable declared outside a procedure, can be accessed and changed by any script in the ASP page where it is declared.

A variable declared inside a procedure, is created and destroyed every time the procedure is executed. No scripts outside that specific procedure can access or change that variable.

To make a variable accessible in several ASP pages, declare session variables or application variables.

Session Variables

Session variables stores information about one single user, and are available to all pages in one application. Common information stored in session variables are username and userid. To create a session variable, store it in a Session Object.

Application Variables

Application variables are also available to all pages in one application. Application variables are used to hold information about all users in a specific application. To create an application variable, store it in an Application Object.

Examples

1. Variables are used to store information. This example demonstrates how to create a variable, assign a value to it, and insert the variable value into a text.

Coding

<html>

<body>

<%

Dim name

name="Jan Egil"

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

%>

</body>

</html>

Output

My name is: Jan Egil

2. Arrays are used to store a series of related data items. This example demonstrates how you can make an array that store names.

Coding

<html>

<body>

<%

Dim famname(5)

famname(0) = "Jan Egil"

famname(1) = "Tove"

famname(2) = "Hege"

famname(3) = "Ståle"

famname(4) = "Kai Jim"

famname(5) = "Bųrge"

For i = 0 to 5

response.write(famname(i) & "<br>")

Next

%>

</body>

</html>

Output

Jan Egil

Tove

Hege

Ståle

Kai Jim

Bųrge

3. This example demonstrates how you can loop through the 6 headers in HTML.

Coding

<html>

<body>

<%

Dim i

for i = 1 to 6

response.write("<h" & i & ">This is header " & i & "</h" & i & ">")

next

%>

</body>

</html>

Output

This is header 1

This is header 2

This is header 3

This is header 4

This is header 5
This is header 6

4. How to write VBScript syntax in ASP. This example will display a different message to the user depending on the time on the server.

Coding

<html>

<body>

<%

Dim h

h = hour(now())

If h < 12 then

response.write("Good Morning!")

else

response.write("Good day!")

end if

%>

</body>

</html>

Output

Good Morning!

5. How to write JavaScript syntax in ASP. This example is the same as the one above, but the syntax is different.

Coding

<%@ language="javascript" %>

<html>

<body>

<%

var d = new Date()

h = d.getHours()

if (h < 12)

{

Response.Write("Good Morning!")

}

else

{

Response.Write("Good day!")

}

%>

</body>

</html>

Output

Good Morning!

Cheap Web Hosting Articles - Web Site Design & Web Hosting Tutorials - Domain Hosting