Ho una classe per gestire le variabili di sessione. Ecco un esempio in allegato:Come ottenere il valore della sessione utilizzando javascript
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; }
}
}
}
Ora, a un certo punto ho memorizzare il flightID
in:
General.Session.departFlightID = flightID;
E in un altro punto che voglio recuperare questo valore utilizzando Javascript. Ho visto esempi qui, ma non funzioneranno (non c'è errore ma restituiranno EMPTY
).
più recenti:
var session = '<%= General.Session.departFlightID %>';
var session = '<%= HttpContext.Current.Session["departFlightID"] %>';
funziona se ho impostato il valore al caricamento della pagina, ma sto in esecuzione un webmethod in cui creo un html e poi inviare tale html torna a essere popolato all'interno di un div a fai un biglietto lì. Devo ottenere il valore della sessione ma non si aggiorna.
Per renderlo più chiaro qui è il codice Javascript:
<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;
}
}
E qui è il 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;
}
Hai provato: 'var session = '<% = HttpContext.Current.Session [ "departFlightID" ]; %> '; '? – karaxuna
@karaxuna no ma fammi controllare ... – confusedMind
commento modificato .. – karaxuna