如何访问身后会话对象的成员变量一个asp.net code在JavaScript? [英] How do you access an asp.net code behind session object member variable in javascript?
问题描述
我有一个处理我所有的会话变量在我的asp.net应用程序的类。此外,我有时在会话变量存储对象,从而让我code非常相似,一个正常的.NET应用程序。
I have a class that handles all of my session variables in my asp.net application. Moreover, I sometimes store objects in the session variables so as to allow me to code very similar to a normal .net application.
例如,这里是一个对象在会话变量:
For example, here is an object in a session variable:
public cUser User
{
get { return (cUser)HttpContext.Current.Session["CurrentUser"]; }
set { HttpContext.Current.Session["CurrentUser"] = value; }
}
这是MySessionClass的实例:
And here is the instance of MySessionClass:
public static MySessinClass Current
{
get
{
MySessionClass session = (MySessionClass)HttpContext.Current.Session["MySessionClass"];
if (session == null) {
session = new MySessionClass();
HttpContext.Current.Session["MySessionClass"] = session;
}
return session;
}
}
所以,在后面的aspx页面我的code,我只想做一些诸如
So, in my code behind aspx page, I would just do something such as
int userID = MySessionClass.Current.User.UserID;
这个伟大的工程,我喜欢它。不过,我想从我的aspx页面在JavaScript中采用同样的原则:
This works great and I love it. However, I want to apply the same principle in javascript from my aspx page:
var userID = <%=MySessionClass.Current.User.UserID%>;
然而,当我访问该网页,我得到一个错误说,它不承认MySessionClass对象。
However, when I access that webpage, I get an error saying that it does not recognize the MySessionClass object.
所以,考虑到我的背景下,这将是引用一个用户名变量,从对象,从会话,在JavaScript中的最佳方式?也许我的语法来引用它关闭或我在它走错了路。任何帮助将是AP preciated,谢谢你。
So, given the context that I am in, what would be the best way to reference that UserID variable, from the object, from the session, in javascript? Maybe my syntax to reference it is off or I am going at it the wrong way. Any help would be appreciated, thank you.
推荐答案
既然你已经将它定义为一个静态成员,您将需要的页面类型限定您参考:
Since you've defined it as a static member, you would need to qualify your reference with the page type:
var userID = <%=MyPageType.MySessionClass.Current.User.UserID%>;
这应该够了。
这篇关于如何访问身后会话对象的成员变量一个asp.net code在JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!