Reputation: 69
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.
Upvotes: 1
Views: 3919
Reputation: 2569
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
Upvotes: 0
Reputation: 2989
You should be able to access the Session variable form all pages in the following way:
var username = Session["Username"].ToString();
Hope this helps
Upvotes: 1
Reputation: 4962
Use session["username"]
to get the value. Then use this value as per your need
Upvotes: 0
Reputation: 8397
You can access your current session variables using the Session object with an index, like
var myvalue = Session["mysessionvariable"];
Upvotes: 0