Freigeben über


How to: Save Values in Session State

This example uses the HttpSessionState object to persist values within an individual session.

Example

Dim firstName As String = "John"
Dim lastName As String = "Smith"
Dim city As String = "Seattle"
Session("FirstName") = firstName
Session("LastName") = lastName
Session("City") = city
string firstName = "Jeff";
string lastName = "Smith";
string city = "Seattle";
Session["FirstName"] = firstName;
Session["LastName"] = lastName;
Session["City"] = city;

Compiling the Code

This example requires:

  • A Web Forms page or class that has access to the current request context using the Current property in an ASP.NET application that has session state enabled.

Robust Programming

Session state can expire (by default, after 20 minutes of inactivity), and the information that you store there can be lost. You can control session-state lifetime using the timeout attribute of the sessionState configuration section.

Depending on your application requirements, you may want to consider an alternative to session state for storing information for each user. ASP.NET provides several other options for persisting data within an application. For a comparison of each, see ASP.NET State Management Recommendations.

See Also

Reference

HttpSessionState

Concepts

ASP.NET State Management Overview

ASP.NET View State Overview

ASP.NET State Management Recommendations

ASP.NET State Management Overview