The Session Object
Before you look at the Session object, you need to understand the concept of scope, in which data is either made available to all pages of an ASP.NET application or localized to just certain pages. The Application object example earlier in this chapter (in the section named "Using Application Object Collections: Contents and StaticObjects") demonstrates this concept. You can use the Session object to store both simple data values and ”as you'll see later in this book ”references to objects such as database connections. These become global to the whole application, and are available to any of its pages.
However, when you have data that needs to be shared between different pages, but not necessarily between different clients , you can use the Session object. Each client that requests a page from your application is assigned a Session object. A Session object is created when the client first makes a document request and is destroyed , by default, 20 minutes after the last request was received (or you could use the Abandon method).
Table 4.2 shows the interface to the Session object, and each of the available collections.
Table 4.2. The Salient Session Collections
Session Event Handlers
If you need to write code to be run when the session either starts or finishes, you need to put it into an appropriate event handler. In VB, an event handler takes the following form:
Sub <ObjectName>_<EventName>(<ParameterList>) ... End Sub
Because this sort of script requires a home, you place it in a particular file in the root directory of the virtual mapping, called global.asax. As its name suggests, this file contains information, routines, and variables that are globally available to all the pages in the application. So, if you need to do some processing (such as a database lookup) at the beginning or end of an application, you can put some code in the global.asax. file as follows :
<SCRIPT LANGUAGE="VB" RUNAT="Server"> Sub Session_Onstart ' Insert script to be executed when the session starts End Sub Sub Session_OnEnd ' Insert script to be executed when the session ends End Sub </SCRIPT>
Using Event Handlers to Determine How Long Sessions Live
Determining exactly when a session in ASP.NET starts and ends isn't as obvious as determining the life of a typical program.
In an ASP.NET session, a session starts when a browser first accesses the Web site, or the first time a browser accesses the Web site after the session times out. Because your application is defined as all the documents contained in a single virtual mapping on the server, the session starts the first time any client requests a document from the virtual mapping.
Now that you know how a session starts, all you need to consider now is how it ends. A session stops when the operating system stops the Web server or the session times out after a certain period of inactivity. Then, the script in the OnEnd event handler runs.
Using Session Variables
When creating event handlers for Session_OnStart() and Session_OnEnd() methods , the important question is, "What information are you going to manipulate?" The answer is absolutely anything that you might want to use in your scripts in other parts of the application during a session, which consequently need to be Global or Public. One obvious value is the time that the session started.
To store global information in the Session object, you must assign that information a name. The name becomes the key by which you access that value in the future. You can think of it as being a custom property of the object. To store the time that the session started, you can add code, such as the DateTime variable within the Session_OnStart() event in the global.asax file, as follows:
<SCRIPT LANGUAGE="VB" RUNAT="SERVER"> Sub Session_OnStart Session.Value ("startDateTime") = Now () End sub </SCRIPT>
The following is part of a sample page, AppObj.aspx, that uses this stored time value, and also creates a global value of its own:
<H1>Session Started at: <% response.write(Session("startDateTime")) %> </H1>