DekGenius.com
[ Team LiB ] Previous Section Next Section

19.5 Events Reference

Start

Sub Session_OnStart( )
 'Event handler logic
End Sub

Fired when the session is created. The event handler for this event should be defined in the global.asax application file.

Parameters

None

Example

The example writes an entry to both the Application Event log and the IIS log for the application to indicate that the Start event has fired:

<Script language="VB" runat="server">
   Sub Session_OnStart( )
      Dim EventLog1 As New System.Diagnostics.EventLog ("Application", _
         ".", "mySource")
      EventLog1.WriteEntry("Session_OnStart fired!")
      Context.Response.AppendToLog("Session_OnStart fired!")
   End Sub
</script>

Notes

The Start event is useful for performing initialization tasks for a new session. One limitation of classic ASP, the inability to access the Server. MapPath method in the Session_OnStart event handler, is eliminated in ASP.NET. You can now successfully call Server.MapPath from within this event handler.

End

Sub Session_OnEnd( )
 'Event handler logic
End Sub

Fired when the session is torn down—either by calling the Abandon method or when the Session.Timeout value expires. The event handler for this event should be defined in the global.asax application file.

Parameters

None

Example

The example below writes an entry to the Application Event log to indicate that the End event has fired:

<Script language="VB" runat="server">
   Sub Session_OnEnd( )
      Dim EventLog1 As New System.Diagnostics.EventLog ("Application", _
         ".", "mySource")
      EventLog1.WriteEntry("Session_OnEnd fired!")
      ' Response is not available in this event handler
      ' Context.Response.AppendToLog("Session_OnEnd fired!")
   End Sub
</script>

Notes

The End event is useful for performing cleanup tasks when the user's session ends—either when the Abandon method is called or when the session times out. Note that the Response object is not available in the context of the Session_OnEnd event handler. Unlike Session_OnStart, the Server.MapPath method is not available. Attempts to access the Response object or the Server.MapPath method from within this event handler will result in an exception being thrown. Since there is no context for displaying exception information, you will not see an error message. You can handle an exception thrown in the Session_OnEnd event handler by creating a handler for the Application.Error event, as shown below:

Sub Application_OnError( )
   Dim EventLog1 As New System.Diagnostics.EventLog ("Application", _
      ".", "mySource")
   EventLog1.WriteEntry("Error Occurred. Error info:" &    Server. GetLastError( ).ToString( ))
End Sub

Note that the End event will be fired only when session state has been configured for in-process operation.

    [ Team LiB ] Previous Section Next Section