Page

8.4.1- Session Object Collections

  by NT Community Manager.
Last Updated  by NT Community Manager.  

PublicCategorized as 08. Applications, Sessions and Cookies.

Not tagged.
<< 8.4.0- The Session ObjectChapter88.4.2- Session Object Properties >>

Session Object Collections

Like the Application object, the Session object has two collections:

 

  • Contents collection
  • StaticObjects collection

Contents Collection

The Contents collection contains all the variables established for a session without using the <OBJECT> tag. The Contents collection is used to determine the value of a specific session item, or to iterate through the collection and retrieve a list of all items in the session.

 

Session.Contents("Key")

 

Where "Key" stands for a specific variable's name.

 

For example:

 

<%= Session.Contents("VisitorID")%>

 

For single session variables, because contents is the default collection, this is usually shortened to:

 

<%= Session("VisitorID")%>

Both code lines will produce the same result.

 

You can iterate through the Session.Contents collection with the following code:

 

<%

For Each item in Session.Contents

If IsObject(Session.Contents(item)) Then

Response.write(item & " : Can't display object" & "<BR>")

Else

If IsArray(Session.Contents(item)) Then

Response.write "Array: " & Session.Contents(item)

For each objArray in Session.Contents(item)

Response.write "<LI>" & _

Session.Contents(item)(objArray)& "<BR>"

Next

Response.write "</LI>"

Else

Response.write(item & " : " & Session.Contents(item) & "<BR>")

End If

End If

Next

%>

 

This code will produce a list of all the Session variables' names and values. The For Each statement will iterate through each of the elements in the Contents collection. As you can store objects or arrays in the collection, we need to check for these specific occurrences and handle them. You can't display an object, so we need to just mention that a certain item is an object. You also need to iterate through an array and display each element. Finally you need to display the 'normal' items. Each of these elements will be a key into the collection. You can then display this key, as well as the element in the collection associated with that key. The Next statement will take you to the next key in the collection, or if you are at the end, it will take you to the next line in the script.

StaticObjects Collection

The Session StaticObjects collection contains all the objects created with the <OBJECT> tag within session scope. The StaticObjects collection is used to retrieve the value for an object's specific property, or to iterate through the collection and retrieve all properties for all objects.

<< 8.4.0- The Session ObjectChapter88.4.2- Session Object Properties >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |