Asp.net session variable Asp.net session variable asp.net asp.net

Asp.net session variable


Yes static variables are shared by the whole application, they are in no way private to the user/session.

To access the Session object from a non-page class, you should use HttpContext.Current.Session.


GlobalVariable is a misleading name. Whatever it's called, it shouldn't be static if it's per session. You can do something like this instead:

// store the selected productthis.Session["CurrentProductId"] = productId;

You shouldn't try to make the Session collection globally accessible either. Rather, pass only the data you need and get / set using Session where appropriate.

Here's an overview on working with session storage in ASP .NET on MSDN.


You sort of answered your own question. An answer is in session variables. In your GlobalVariable class, you can place properties which are backed by session variables.

Example:

public string SelectedProductName {    get { return (string)Session["SelectedProductName"]; }    set { Session["SelectedProductName"] = value; }}