C# 如何使用javascript获取会话值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17700296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get session value using javascript
提问by confusedMind
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:
现在在某些时候我将其存储flightID
在:
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
).
在另一点上,我想使用 Javascript 检索此值。我在这里看到过示例,但它们不起作用(没有错误,但它们会返回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.
如果我在页面加载时设置值,它确实有效,但我正在运行一个 webmethod,我在其中创建一个 html,然后将该 html 发送回填充到 div 中以在那里制作票证。我需要获取会话值,但它不会更新。
To make it more clear here is the Javascript code:
为了更清楚地说明这里是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;
}
}
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;
}
采纳答案by R.C
This 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.
我们通常在会话变量中有敏感数据,所以它们总是在服务器端。尽管如此,如果您愿意,您可以尝试在您的 .aspx 标记中添加一个 HiddenField,您可以在后面的代码中设置它。一旦设置了值,您就可以非常轻松地使用 $("#id") 方式或 Document.GetElementById() 方法访问该值。
回答by Matthew Schaad
WebMethods do not support Session variables by default. Your WebMethod declaration should look like this:
默认情况下,WebMethods 不支持会话变量。您的 WebMethod 声明应如下所示:
[WebMethod(EnableSession = true)]
回答by anji
If you are using mvc, then:
如果您使用的是 mvc,则:
var ttype = ' @HttpContext.Current.Session["TestType"] ';