Difficulty: Moderately Easy
Things You’ll Need:
- ASP.NET
- Microsoft Visual Studio 2005
- A computer with IIS and ASP2.0 setup and/or
- A hosting company with ASP.NET support
Step1
Add Global.asax
Plan ahead. Session states can be created on the fly, but it's always better to plan their use to maximize their effectiveness. One of the best places to state your session states is in the "Global.asax" file within your web application. To create this file, right-click on your project title in the Solution Explorer and select "Add New Item". From the window, select "Global Application Class".
Step2
Add a Session State
The Session States can really be placed anywhere in this file. The most basic spot is the Session_Start. Declare an empty Session State by the following: Session("DescriptiveName") = "". Add as many as you want, even assign values if you know what they are ahead of time.
Step3
Adding the extras
In your "Default.aspx" file, write the Session("Greeting") to the screen and drop in a TextBox and button. Lastly, write the Session("UserName") to the screen. In the example, a few more details were added for clarity, including an OnClick function for the button.
Step4
Run the example
Run the example. Notice the Greeting state that was declared in "Global.asax" is immediately displayed while the UserName state is not. Now, if you type in the TextBox and click the button, the page will reload and the UserName state will now contain what you typed in the TextBox. In either case, you will have successfully captured, used, and displayed information that never had to be written to a cookie on your guest's computer.