Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Use Session Object as Static Class Variables

0.00/5 (No votes)
5 Nov 2013 2  
This tip describes the way to use the session object as a static class.

Introduction

This tip describes the way to use the session object as a static class. The advantage is that you can define all the variables (with their appropriate getters / setters) that you will require and session values can be set / get using . (dot) operator of static class.

Using the Code

You can use session object as static class using the appcode folder. You need to define a static class with its member variables and getters / setters. Just follow the steps given below:

  1. Create a new .cs class in your appcode folder.
  2. Convert the class as static:
    public static class SessionManager 
  3. Declare and define the static variables names:
    private const string SESSION_StageFlag = "StageFlag";
  4. Create the appropriate properties for each variable defined earlier:
    public static string StageFlag
            {
                get
                {
                    if (null != HttpContext.Current.Session[SESSION_StageFlag])
                        return HttpContext.Current.Session[SESSION_StageFlag] as string;
                    else
                        return null;
                }
                set
                {
                    HttpContext.Current.Session[SESSION_StageFlag] = value;
                }
            }
  5. Build your project and now you will be able to manipulate the session through SessionManager in your aspx.cs classes. Just add the namespace of your new SessionManager class and it can be accessed as shown below:
    SessionManager.StageFlag = "Fixed" 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here