Saturday, April 3, 2010

To make a new class and save session variables in this class


public class MySession
{
    // private constructor
    private MySession()
    {
        //implement constructor if you want
    }

    public static MySession Current
    {
        get
        {
            MySession session = (MySession)HttpContext.Current.Session["__MySession__"];
            if (session == null)
            {
                session = new MySession();
                HttpContext.Current.Session["__MySession__"] = session;
            }
            return session;
        }
    }

    // **** Add your session properties here
    //public string Property1 { get; set; }
    //public DateTime MyDate { get; set; }
    //public int LoginId { get; set; }

    //***** You can use these properties from anywhere like this
    //    int loginId = MySession.Current.LoginId;

    //string property1 = MySession.Current.Property1;
    //MySession.Current.Property1 = newValue;

    //DateTime myDate = MySession.Current.MyDate;
    //MySession.Current.MyDate = DateTime.Now;
    public string CurrentUserName { get; set; }
    public string CurrentPageTitle { get; set; }



}

No comments:

Post a Comment