Reputation: 51
I am trying, to avoid using querystrings, to use a session variable to pass the desired value. I'm doing this from a class to a handler.
In the class I use:
HttpContext.Session["name"] = "Value";
In the handler I use:
var name = context.Session["name"];
I have already added IReadOnlySessionState
or IRequiresSessionState
but the session variables remain null.
Some help?
Upvotes: 1
Views: 1276
Reputation: 1
I realize this is an old post, however others may find this useful.
Try passing HttpContext.Current as a parameter to the routine in your common class.
Code from the web forms class:
protected void SetSessionValue()
{
var CurrentSession = HttpContext.Current;
oCommonClass.cSetSessionValue(CurrentSession);
var SessionValue = Session("SessionValue");
}
Code from the common class:
public void cSetSessionValue(object cCurrentSession)
{
var cSessionValue = "New Value";
cCurrentSession.Session("SessionValue") = cSessionValue;
}
Upvotes: 0
Reputation: 138
I think you are creating new context again. you should use the same session using HttpContext.Current
.
Try Code Like below and Refer Comments what i have added below.
string firstName = "Jeff";
string lastName = "Smith";
string city = "Seattle";
// Save to session state in a Web Forms page class.
Session["FirstName"] = firstName;
Session["LastName"] = lastName;
Session["City"] = city;
// Read from session state in a Web Forms page class.
firstName = (string)(Session["FirstName"]);
lastName = (string)(Session["LastName"]);
city = (string)(Session["City"]);
// Outside of Web Forms page class, use HttpContext.Current.
HttpContext context = HttpContext.Current;
context.Session["FirstName"] = firstName;
firstName = (string)(context.Session["FirstName"]);
Upvotes: 1