Asp. NET state management nine (session sessions)

Source: Internet
Author: User
Tags microsoft sql server sessions

asp.net allows you to save the value of each active WEB application session using session state, which is an instance of the HttpSessionState class.

Session state is similar to application state, except that the scope of session state is limited to the current browser session. If you have different users using your application, each user session will have a different session state. Also, if the same user returns to the application after exiting, the session state of the second user session will be different from the first.

Session state uses a key/value dictionary structure to store session-specific information that needs to be maintained between server round trips and page requests.

You can use session state to accomplish the following tasks:

1. Uniquely identifies a browser or client device request and maps these requests to a separate session instance on the server.

2. Store session-specific data on the server for multiple browsers or client device requests within the same session.

3. Raises the appropriate session management event. In addition, you can use these events to write application code.

Once the application-specific information is added to session state, the server manages the object. Depending on the options you specify, you can store session information in a Cookie, in an out-of-process server, or on a computer running Microsoft SQL Server.

By default, all ASP.net applications enable ASP.net session state.

Session variables

How to: Save values in Session state

Session variables are stored in sessionstateitemcollection that are exposed through the System.Web.HttpContext.Session property.

The session variable collection is indexed by variable name or integer index. You can create session variables by referencing session variables by name only. You do not need to declare a session variable or explicitly add a session variable to the collection.

For example, the following code example creates session variables that represent the user's first and last names, and sets them to the values retrieved from the TextBox control.

session["FirstName"] = Firstnametextbox.text;
session["LastName"] = Lastnametextbox.text;

By default, session variables can be any valid. NET type.

For example, the following code example stores the ArrayList of a value in a session variable named "StockPicks." Note that the value returned by the "StockPicks" Session variable must be cast to the appropriate type when retrieved from Sessionstateitemcollection.

ArrayList stockpicks = (ArrayList) session["StockPicks"];
session["stockpicks"] = stockpicks;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.