I have a class to deal with session variables. Here is a sample attached:
namespace General
{
public class Session
{
public Session()
{
}
public static string UserID
{
get { return HttpContext.Current.Session["UserID"] as string ?? String.Empty; }
set { HttpContext.Current.Session["UserID"] = value; }
}
public static string departFlightID
{
get { return HttpContext.Current.Session["departFlightID"] as string ?? String.Empty; }
set { HttpContext.Current.Session["departFlightID"] = value; }
}
public static string returnFlightID
{
get { return HttpContext.Current.Session["returnFlightID"] as string ?? String.Empty; }
set { HttpContext.Current.Session["returnFlightID"] = value; }
}
}
}
Now at some point I store the flightID
in:
General.Session.departFlightID = flightID;
And at another point I want to retrieve this value using Javascript. I have seen examples here but they won't work (there's no error but they will return EMPTY
).
Most recent tries:
var session = '<%= General.Session.departFlightID %>';
var session = '<%= HttpContext.Current.Session["departFlightID"] %>';
It does work if I set value on page load, but I am running a webmethod where I create a html and then send that html back to be populated inside a div to make a ticket there. I need to get the session value but it does not update.
To make it more clear here is the Javascript code:
<script type="text/javascript">
function addTicket(flightID, flightType) {
PageMethods.addTicket(flightID, flightType, OnGetMessageSuccess);
}
function OnGetMessageSuccess(result, userContext, methodName) {
var pageUrl = document.URL;
if (pageUrl.indexOf("&ret_date=&") !== -1) {
var session = '<%= HttpContext.Current.Session["departFlightID"] %>';
alert(session);
result = result + "<a onclick=\"searchflight.abortAllAjax()\" data-index=\"6\" class=\"bookNow\" title=\"Book Now!\" href=\"#\">Book Now!</a>";
result = result + "</div> </div> </div>";
document.getElementById("detail").innerHTML = result;
}
}
And here is the webmethod:
[System.Web.Services.WebMethod]
public static string addTicket(string flightID, string flightType)
{
//GET FLIGHT DETAILS FROM DATATABLE OR SESSION DATA TABLE
string tmpHtml = string.Empty;
tmphtml="sample";
string flightID="123123213";
General.Session.departFlightID=flightID;
return tmphtml;
}
I have a class to deal with session variables. Here is a sample attached:
namespace General
{
public class Session
{
public Session()
{
}
public static string UserID
{
get { return HttpContext.Current.Session["UserID"] as string ?? String.Empty; }
set { HttpContext.Current.Session["UserID"] = value; }
}
public static string departFlightID
{
get { return HttpContext.Current.Session["departFlightID"] as string ?? String.Empty; }
set { HttpContext.Current.Session["departFlightID"] = value; }
}
public static string returnFlightID
{
get { return HttpContext.Current.Session["returnFlightID"] as string ?? String.Empty; }
set { HttpContext.Current.Session["returnFlightID"] = value; }
}
}
}
Now at some point I store the flightID
in:
General.Session.departFlightID = flightID;
And at another point I want to retrieve this value using Javascript. I have seen examples here but they won't work (there's no error but they will return EMPTY
).
Most recent tries:
var session = '<%= General.Session.departFlightID %>';
var session = '<%= HttpContext.Current.Session["departFlightID"] %>';
It does work if I set value on page load, but I am running a webmethod where I create a html and then send that html back to be populated inside a div to make a ticket there. I need to get the session value but it does not update.
To make it more clear here is the Javascript code:
<script type="text/javascript">
function addTicket(flightID, flightType) {
PageMethods.addTicket(flightID, flightType, OnGetMessageSuccess);
}
function OnGetMessageSuccess(result, userContext, methodName) {
var pageUrl = document.URL;
if (pageUrl.indexOf("&ret_date=&") !== -1) {
var session = '<%= HttpContext.Current.Session["departFlightID"] %>';
alert(session);
result = result + "<a onclick=\"searchflight.abortAllAjax()\" data-index=\"6\" class=\"bookNow\" title=\"Book Now!\" href=\"#\">Book Now!</a>";
result = result + "</div> </div> </div>";
document.getElementById("detail").innerHTML = result;
}
}
And here is the webmethod:
[System.Web.Services.WebMethod]
public static string addTicket(string flightID, string flightType)
{
//GET FLIGHT DETAILS FROM DATATABLE OR SESSION DATA TABLE
string tmpHtml = string.Empty;
tmphtml="sample";
string flightID="123123213";
General.Session.departFlightID=flightID;
return tmphtml;
}
Share
Improve this question
edited Aug 12, 2015 at 20:14
Null
1,9709 gold badges31 silver badges35 bronze badges
asked Jul 17, 2013 at 12:52
confusedMindconfusedMind
2,6537 gold badges34 silver badges78 bronze badges
6
-
have you tried:
var session = '<%= HttpContext.Current.Session["departFlightID"]; %>';
? – karaxuna Commented Jul 17, 2013 at 12:54 - @karaxuna nope but let me check ... – confusedMind Commented Jul 17, 2013 at 12:55
- yup it did the same returns empty. – confusedMind Commented Jul 17, 2013 at 12:58
- are you writing above script code in aspx file or js file ? if you are writing it in a js file it will not work. – Shivkumar Deshmukh Commented Jul 17, 2013 at 13:10
-
maybe it's because of static context. try assigning like this:
HttpContext.Current.Session["departFlightID"] = flightID;
– karaxuna Commented Jul 17, 2013 at 13:12
3 Answers
Reset to default 2This is the same Question as here.
we usually have sensitive data in Session Variables, so they are always server side. Still if you want you can try adding a HiddenField in your .aspx markup and in code behind you can set it . once the value is set, you can very easily access the value using $("#id") way or Document.GetElementById() method.
If you are using mvc, then:
var ttype = ' @HttpContext.Current.Session["TestType"] ';
WebMethods do not support Session variables by default. Your WebMethod declaration should look like this:
[WebMethod(EnableSession = true)]