我有一个处理会话变量的类。以下是附件中的示例:
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,以便在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;
}发布于 2013-07-18 00:28:53
默认情况下,WebMethods不支持会话变量。您的WebMethod声明应如下所示:
[WebMethod(EnableSession = true)]https://stackoverflow.com/questions/17700296
复制相似问题