1

I have a field named username as the session variable. I have added a class which inherits the base page. Now I want the code to get the session variable in all the pages that the user moves through.

Please help me with the code.

Anders Arpi
  • 8,277
  • 3
  • 33
  • 49
saaswathy
  • 69
  • 2
  • 7

4 Answers4

1

You should be able to access the Session variable form all pages in the following way:

 var username = Session["Username"].ToString();

Hope this helps

Gaz Winter
  • 2,924
  • 2
  • 25
  • 47
0

You can access your current session variables using the Session object with an index, like

var myvalue = Session["mysessionvariable"];
Anders Arpi
  • 8,277
  • 3
  • 33
  • 49
0

Use session["username"] to get the value. Then use this value as per your need

Shiridish
  • 4,942
  • 5
  • 33
  • 64
0

You can add a property in a base class (which is inherited from Page class) which will encapsulate the Session variable and inherit that base class in every page you create

public string UserNameInSession 
{
    get 
           { 
              return HttpContextCurrent["UserNameSessionKey"].ToString();
           }
    set 
           { 
              HttpContextCurrent["UserNameSessionKey"] = value; 
           }
}

And then you can use this property either to set or get the Username from/to session like

string UserName = UserNameInSession; //Get it
UserNameInSession = string.Empty();//set it  
Pravin Pawar
  • 2,559
  • 3
  • 34
  • 40