0

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; }
}

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;
        }
    }

So, in my code behind aspx page, I would just do something such as

int userID = MySessionClass.Current.User.UserID;

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%>;

However, when I access that webpage, I get an error saying that it does not recognize the MySessionClass object.

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.

1 Answer 1

3

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%>; 

That should be enough.

Sign up to request clarification or add additional context in comments.

3 Comments

I am not sure what you mean by page type. The "MySessionClass" is not a page, but a simple MySessionClass.cs file. It uses the HttpContext.Current.Session to access the session variables
+1 The page type is on top of your .aspx.cs file, f.e. public partial class YourPageType : Page. If your MySessionClass is in a different file, specify it's namespace, f.e. <%= MyNamespace.MySessionClass.Current.User.UserID%>
How are we meant to know that? Please publish all relevant code then. From your current example, I don't know if Current is a member on your page or this static session class. Additionally, do you have the right namespaces imported in your ASPX page directivecs?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.