Name

Abandon

Synopsis

Session.
                     Abandon( )

Immediately terminates the current user’s session and causes the Session.End event to be fired.

Parameters

None

Example

The example examines the IsNewSession property to determine if the current request has resulted in a new session. If so, it adds a value to the Session collection and then displays a message indicating that a new session was created. If a session already exists, the example displays a button that, when clicked, causes a 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 ...

Get ASP.NET in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.