Tuesday, February 5, 2008

Accessing session state and application state variables from classes which are not code-behind classes in your project.

In some cases developers decide to use application or session state variables in classes that are not code-behind classes. One of the examples of this situation is when programmer chooses to have a common class for all the web pages to do one specific job.

The following property of HttpContext class allows you to do just that:

System.Web.HttpContext.Current

The above property returns System.Web.HttpContext object for current HTTP request.

You can also have access to current Request, Context, Response, Server, and Page classes.

Example:

System.Web.HttpContext.Current.Session["Key"]="Value";

No comments: