Name

IsNewSession

Synopsis

Boolean = Session.IsNewSession

Returns a Boolean indicating whether the current session was created as a result of the current request.

Parameters

Boolean

A Boolean variable that will receive the IsNewSession property value. Returns True on the request that creates a Session and False for each subsequent request from the same client.

Example

The example tests to see if the current request created a new session and if so, adds a value to the Session collection and then displays a message containing the SessionID of the current session:

Sub Page_Load(  )
   If Session.IsNewSession Then
      Session("foo") = "foo"
      Message.Text = "The current Session (SessionID: " & _
         Session.SessionID & ") was created with this request."
   Else
      Message.Text = "The current Session (SessionID: " & _
         Session.SessionID & ") existed prior to this request."
   End If
End Sub

Notes

The IsNewSession property is very useful when you want to initialize Session collection items for only certain pages. Unlike the Session_OnStart event handler in global.asax, which is called when a session is created, regardless of which page creates the session, this property gives you finer-grained control over initialization and session behavior.

As mentioned in the introduction to this chapter, while a new SessionID is generated for each request that does not already have a session, a new session is not created for a given request unless the requested page stores a value in the Session collection or an event handler ...

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.