Visual InterDev

Sharing Dynamic Information

See Also

Many times in your Web applications you will need to use the same information on two or more pages. For example, your application might:

Note   You can use the scripting object model and page objects to maintain state information automatically. For details, see The Scripting Object Model.

To share dynamic information

Each method has different uses, depending on factors such as how many pages might need the information, and whether you need to keep it permanently.

Storing Information Globally

You can define global variables with two Microsoft® Internet Information Services (IIS) objects:

To get and set values in Application and Session object variables

To initialize Application and Session object variables

For example, you can maintain a hit counter in an Application object variable. You can initialize the counter in the Application_OnStart event using a script such as this:

Sub Application_OnStart()
   Application("counter") = 0
End Sub

Each time a user starts a new session, the counter is updated. You then also make a local copy of the counter in a Session object variable for the user. The best place to do this is in the Session_OnStart event of the Global.asa file, using script such as this:

Sub Session_OnStart()
   iCount = Application("counter")
   iCount = iCount + 1
   Application("counter") = iCount   ' Global counter
   Session("counter") = iCount   ' User's copy of counter
End Sub

The opening page of your application could display the counter in this way:

<BODY>
<H1>Welcome</H1>
You are visitor number <%=Session("counter")%> 
out of <%=Application("Counter")%>.
</BODY>

When the user sees the opening page the first time, the two numbers are the same. If the user revisits the page, the "out of" number (the Application object counter) might change if other users have started sessions since the user first started a session.

Application and session object variables generally store dynamic information only. If you want to keep the information permanently, you must devise a way to save the information between applications and sessions. One way is to write out values in Application_OnEnd or Session_OnEnd handlers in the Global.asa file.

Adding Query Strings to Links

You can pass information directly to another page as part of a link (<A> tag). For example, a page might display a list of employee names, each of which is a link. When the user selects one of the names, the link calls a page and passes it the corresponding employee ID.

To add a query string to a link

The following example shows a page that displays a list of employee names. Each name is a link. All links go to the same page, but each one passes a different employee ID.

<BODY>
<H1>List of employees</H1>
<P>Click the name of an employee to see information about that employee.</P>
<A HREF=EmplInputForm.asp?empid=1>Ann</A><BR>
<A HREF=EmplInputForm.asp?empid=2>John</A><BR>
<A HREF=EmplInputForm.asp?empid=3>Susan</A><BR>
<A HREF=EmplInputForm.asp?empid=4>Michael</A><BR>
</BODY>

In the EmplInputForm.asp file, you can use the QueryString collection of the server Request object to determine what value was passed:

<% vEmplID = Request.QueryString("empid") %>

You can create more sophisticated dynamic links by using server script to supply values. For example, the following is a single line in which both the employee ID and employee name are supplied from a database query:

<A HREF=EmplInputForm.asp?empid= <%=RS.Fields("EmpID")%> > _
   <%=RS.Fields("EmpFName")%> </A>

For more information about getting information from a database, see the Microsoft ActiveX Data Objects (ADO) Overview.

Storing Information on the User’s Computer

A convenient way to maintain information about a user is to use a cookie. When a session is first started, the server sends a cookie to the client browser. Each time the client browser requests a page from that server, it sends the cookie back to the server, which can then read the cookie and identify the client browser.

You can use cookies to store your own application information, such as user preferences. Cookies are available until the date specified in the cookies' Expires attribute. For information about cookie persistence, see the Active Server Pages documentation.

Note   Because cookies can potentially write to the user's hard disk, most browsers usually allow users to disable them or to display a warning before accepting a cookie. For some applications — such as public applications that might be accessed by users with a wide range of browsers and security settings — cookies can be impractical. If you do use cookies, your application must provide an alternative way to maintain dynamic information if the user's browser refuses a cookie.

To store information in a cookie

Cookies can store multiple values. Each value in the cookie is assigned a key by which you identify it.

To store a value in a cookie, you use the Response object, specifying the name of the cookie to update, the key to update, and the value. If the cookie does not already exist, the Response object creates it. For example, the following updates a cookie setting the key FavoriteColor to the value "Red":

<% Response.Cookies("Preferences")("FavoriteColor")="Red" %>

Note   You must update cookies in the <HEAD> section of an .asp file, or an error will result.

If an existing cookie has key values but the Response.Cookies method does not specify a key name, then the existing key values are deleted. Similarly, if an existing cookie does not have key values but the Response.Cookies method specifies key names and values, the existing value of the cookie is deleted and new key-value pairs are created.

To retrieve the value from a cookie, you use the Request object with similar syntax:

<% vColor = Request.Cookies("Preferences")("FavoriteColor") %>

To see how you can use cookies to store information, see the User Preferences Sample.

Reading from and Writing to Files

In server scripts, another way to maintain information is to store it in a text file on the server.

To read and write text files

  1. In a server script, create a TextStream object.

  2. Use the object's CreateTextFile, WriteLine, and ReadLine methods to manage the information in the file.

The following example creates a new file and writes a single line of text to the file:

<HTML>
<BODY>
<H3>Textstream test</H3>
<%
  Set OutStream = Server.CreateObject("MS.TextStream")
  OutStream.CreateTextFile "tsworks.txt", , True
  OutStream.WriteLine "This line is written to the file."
%>
</BODY>
</HTML>