ASP.NET

Getting a Taste of Session State

To see how session state works, here's an exercise that involves creating a Web site whose page stores a value as a member variable in the page and as an element of session state. It will illustrate the difference between page state during a request and session data that persists beyond a request.

Trying Session State

  1. Create a new Web site. Name it SessionState. Make sure it's an HTTP site so you may use IIS to configure the session state.

  2. In the default page (Default.aspx), drag a text box (and a label to identify the TextBox if you want) onto the form. Then drag two buttons and another label onto the form like so:

    Graphic
  3. Name the first button Submit String. That is, set the ID to SubmitString. It doesn't matter what you name the second button. The first button will submit the string to the form, and the other button will just perform a post back. That way, you'll be able to see the ephemeral nature of page member variables. Name the label LabelShowString. We'll use it to display the value of the string.

  4. Add a String variable member to the page. In the Page_Load handler, set the text box on the page to the value of the string. Then add a handler for the Submit String button. Have the handler take the Text property from the TextBox1 and store it in the page member variable. Then set the LabelShowString label text to the value of the string like so:

    public partial class _Default : System.Web.UI.Page
    {
    
       String _str;
    
       protected void Page_Load(object sender, EventArgs e)
       {
         this.LabelShowString.Text = this._str;
    
        }
       protected void SubmitString_Click(object sender, EventArgs e)
       {
    
         this._str = this.TextBox1.Text;
         this.LabelShowString.Text = this._str;
       }
    }
    
  5. Now run the program. Type a string into the text box and click Submit String. When the post goes to the page, the page will show the string in the label.

    Graphic
  6. Now click the Submit String button. What happens? Remember, Page_Load simply looks at the value of the _str member variable and stuffs it into the label. Pages (and HTTP handlers in general) are very short-lived objects. They live for the duration of the request and then are destroyed-along with all the data they hold. The _str member variable evaporated as soon as the last request finished. A new _str member variable (which was empty) was instantiated as soon as the page was recreated.

    Graphic

    To sum up, we saw in Tutorial 4 that controls manage their own state. But in this case, we're taking the data from the text box and storing it in a member variable in the Page class. The lifetime of the page is very short. The page lives long enough to generate a response, then it disappears. Any state you've stored as data members in the page disappears, too.

  7. Session state is a way to solve this issue. To show this, add a new label to the page. This one will show the data as retrieved from the Session object.

    Graphic
  8. Write code to store the string in session state. Have the SubmitString take the text from the TextBox1 and store it into the Session object. Then update the Page_Load method to display the value as it came from session state as shown below:

    public partial class _Default : System.Web.UI.Page
    {
    
       String _str;
    
       protected void Page_Load(object sender, EventArgs e)
        {
             this.LabelShowString.Text = this._str;
    
             this.LabelShowStringAsSessionState.Text =
                 (String)this.Session["str"];
    
        }
       protected void SubmitString_Click(object sender, EventArgs e)
       {
             this._str = this.TextBox1.Text;
             this.Session["str"] = this.TextBox1.Text;
             this.LabelShowString.Text = this._str;
    
             this.LabelShowStringAsSessionState.Text =
                 (String)this.Session["str"];
    }
    }
    
  9. Run the program. Type in a string and click the Submit String button. Both labels should contain data. The LabelShowString label will hold data because the SubmitString handler made the assignment. The LabelShowStringAsSessionState label also shows data because the handler set that text.

    Graphic
  10. Now click the Just Submit button and see what happens:

    Graphic

    In this case, the page was simply submitted, causing only the Page_Load to be executed. Page_Load displays both the _str member variable (which is blank because it lives and dies with the page) and the data from the Session object (which lives independently of the page).

As you can see, session state is pretty convenient. However, we wouldn't get very far if all we could do was store simple strings and scalars. Fortunately, the session dictionary stores all manner of CLR objects.