
416
|
Chapter 19: The HttpSessionState Class
This is the Title of the Book, eMatter Edition
Copyright © 2008 O’Reilly & Associates, Inc. All rights reserved.
postback. This postback results in the Session.Abandon method being called and
the session terminated:
If Not IsPostBack
If Session.IsNewSession Then
Session("foo") = "foo"
Message.Text = "The current Session (SessionID: " & _
Session.SessionID & ") was created with this request."
Else
Message.Text = "Click the button to abandon the current session."
Dim AbandonButton As New Button
AbandonButton.Text = "Abandon Session"
myForm.Controls.Add(AbandonButton)
End If
Else
Session.Abandon( )
Message.Text = "Session abandoned."
End If
In order for the postback to work correctly, a server-side form needs to be added
within the
<body> tags, as shown below:
<form id="myForm" runat="server">
<asp:label id="Message" runat="server"/>
</form>
Notes
The Abandon method is very important for controlling resource usage in ASP.NET
applications that use session state. If you use session state for storing application
data, you should implement a logout method that calls Session.Abandon and make
it as easy as possible for your users to access this method (via a button or link on
each page). Implementing this method will help prevent resources from being
consumed for longer than necessary when a user has already quit using the
application.
Note that the End event will ...