我有一个类来处理会话变量.以下是附件样本:
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; } } } }
现在在某个时候我将flightID存储在:
General.Session.departFlightID = flightID;
而在另一点我想使用Javascript检索这个值.我已经看到这里的例子,但他们不会工作(没有错误,但他们将返回EMPTY).
最近的尝试:
var session = '<%= General.Session.departFlightID %>'; var session = '<%= HttpContext.Current.Session["departFlightID"] %>';
它的工作,如果我设置页面加载的价值,但我正在运行一个webmethod,我创建一个html,然后发送该html回来填充在一个div,在那里打票.我需要获取会话值,但不会更新.
为了更清楚这里是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; } }
这里是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; }