asp.net state Management: Session, Application and cache

Source: Internet
Author: User
Tags sessions

The previous blog post describes two ways to manage the client state: http://www.cnblogs.com/wolf-sun/p/3329773.html. In addition to saving State on the client, you can save the state on the server. The disadvantage of using the state of the client is that it increases the transmission of data between networks. The disadvantage of using server-side state is that the server must allocate resources to the client, and the server-side state management technology is discussed in detail below.

Server-side state management

Session

Session state is associated with a browser session. The session begins when the customer opens the ASP.net page for the first time on the server. When the customer does not have access to the server within 20 minutes, the session ends and the sessions are destroyed.

You can define your code in the global Application class (that is, the global Application Class), which runs at the beginning or end of the session. The Global.asax file is created. In this file, some time handlers routines are defined:

public class Global:System.Web.HttpApplication
    {
    
        protected void Application_Start (object sender, EventArgs e)
        {
    
        }
    
        protected void Session_Start (object sender, EventArgs e)
        {
    
        }
    
        protected void Application_ BeginRequest (object sender, EventArgs e)
        {
    
        }
    
        protected void Application_AuthenticateRequest (object sender, EventArgs e)
        {
    
        }
    
        protected void Application_Error (object sender, EventArgs e)
        {
    
        }
    
        protected void Session_End (object sender, EventArgs e)
        {
    
        }
    
        protected void Application_End (object sender, EventArgs e)
        {
    
        }
    }

Session state can be stored in the HttpSessionState object. Session-state objects that are related to the current HTTP environment can be accessed using the Page class's sessions property. In the Session_Start () event handler, you can initialize the session variable. In the following instance, the session state named MyData is initialized to 0:

1 protected void Session_Start (object sender, EventArgs e)

2 {

3 session["MyData"] = 0;

4}

Session state can be read in the page by using the session-state name.

protected void Unnamed2_click (object sender, EventArgs e)
        {
            int val = (int) session["MyData"];
            MyLabel.Text = val. ToString ();
            val = 5;
            session["MyData"] = val;
        }

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.