Export (0) Print
Expand All

Page.Session Property

Gets the current Session object provided by ASP.NET.

[Visual Basic]
Public Overridable ReadOnly Property Session As HttpSessionState
[C#]
public virtual HttpSessionState Session {get;}
[C++]
public: __property virtual HttpSessionState* get_Session();
[JScript]
public function get Session() : HttpSessionState;

Property Value

The current session-state data.

Exceptions

Exception Type Condition
HttpException Occurs when the session information is set to a null reference (Nothing in Visual Basic).

Remarks

This property provides information about the current request's session. A Session object is maintained for each user that requests a page or document from an ASP.NET application. Variables stored in the Session object are not discarded when the user moves from page to page in the application; instead, these variables persist as long as the user is accessing pages in your application. For more information about session state, see Session State.

Example

[Visual Basic, C#] The following example creates a GetStyle function that retrieves a key stored in the session state of the Web Forms page and converts its value to a string.

[Visual Basic] 
' Create a private function that obtains
' information stored in session state
' in the application's Global.asax file.
' When this method is called and a key name
' that is stored in session state is passed
' as the parameter, the key is obtained and
' converted to a string.
Function GetStyle(Key As String) As String
  Return Session(Key).ToString()
End Function

[C#] 
// Create a private function that obtains
// information stored in Session state
// in the application's Global.asax file.
// When this method is called and a key name
// that is stored in Session state is passed
// as the paramter, the key is obtained and
// converted to a string.
String GetStyle(String key) {
  return Session[key].ToString();       
}

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

Page Class | Page Members | System.Web.UI Namespace | HttpException

Show:
© 2014 Microsoft