Click here to download the source code for this lesson.
This lesson builds on the previous lesson regarding ASP.NET redirects. In this lesson I will cover the ASP.NET Session object. When you are implementing a requirement that needs information shared among different web pages, you can use the Session object to store state between the group of related pages. There are also situations where you may want to store information in the Session that is shared among ALL web pages in your ASP.NET application. The ASP.NET Session object is another special object that is available to you in addition to the Response object that I covered in the previous lesson. In this lesson I will show you an example of how you can use both of these special objects together to implement a two page credit card validation scenario.
The Session object is similar to a Dictionary object as it contains key/value pairs. You can store as many variables in the Session as you need and they can be of any type, including your own User defined Class types. When you store a variable in the Session, and you want to retrieve it back, use the same key value. For example, if you store an object in the Session using the key “Myvar”, make sure you use “Myvar” when you want to retrieve the object back.
In the example for this lesson, I setup a web form called Page1 that contains some TextBox controls to allow the user to type their name and credit card information. When the user clicks the Next button, I store the information entered into the Session object and redirect the application to another web form called Page2 (which is a confirmation page).
Session["FirstName"] = txtFirstName.Text; Session["LastName"] = txtLastName.Text; Session["CreditCardNumber"] = txtCreditCardNumber.Text; Session["ExpirationDate"] = txtExpirationDate.Text; Response.Redirect("Page2.aspx");
On the Page2 confirmation page, I read back the variables from the Session object and display them on the web page.
txtFirstName.Text = Session["FirstName"].ToString(); txtLastName.Text = Session["LastName"].ToString(); txtCreditCardNumber.Text = Session["CreditCardNumber"].ToString(); txtExpirationDate.Text = Session["ExpirationDate"].ToString();
This is amazing
Comment by Jay Maniar — July 23, 2009 @ 1:43 am
This code is excellent…so easy to use…it worked instantly!
Comment by Max452 — September 12, 2009 @ 4:22 am
Its working fine in IE 7.0 and Mozilla. But not in IE6.0 . Plese give me any solution.
Comment by Sathish — September 21, 2009 @ 8:37 am
Even though iam a beginner , it is easy to me to understand the concept
Comment by charles — November 3, 2009 @ 1:21 am
Amazing.you have done a great job.
Comment by charles — November 3, 2009 @ 1:22 am
Hi, I want some more information which will help me in storing all the session variables in one class name session class… This informations are pretty good but expecting more…
Comment by Bijal — December 29, 2009 @ 11:50 pm
u r done a good job.
Comment by Raghupal K — January 17, 2010 @ 6:10 am
Great job.
Comment by basant — January 19, 2010 @ 2:22 am