con*_*ind 10 javascript c# asp.net session visual-studio-2010
我有一个类来处理会话变量.这是附件样本:
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; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在在某些时候我存储flightID在:
General.Session.departFlightID = flightID;
Run Code Online (Sandbox Code Playgroud)
另一方面,我想使用Javascript检索此值.我在这里看过例子,但是它们不起作用(没有错误,但它们会返回EMPTY).
最近的尝试:
var session = '<%= General.Session.departFlightID %>';
var session = '<%= HttpContext.Current.Session["departFlightID"] %>';
Run Code Online (Sandbox Code Playgroud)
如果我在页面加载上设置值,它确实有效,但是我正在运行一个web方法,我在其中创建一个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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是网络方法:
[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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24280 次 |
| 最近记录: |