LearnFast.Ninja

C Sharp ASP.NET Session variables from custom list trick

Create SessionVariables.cs, a static class with class variables type string. When you call the session and want variable from your list write the following code. When you need value from the session write SessionVariables and dot operator and the Visual Studio IDE will auto complete the names.

Step 1: Create SessionVariables.cs, a static class with class variables type string.

public static class SessionVariables

{

    public static string UserName = "UserName";

    public static string MainMenuCurrent = "MainMenuCurrent";

}// end class


Step 2: When you call the session and want variable from your list write the following code.

Session[SessionVariables.UserName] = "Dani";

// Same as: Session["UserName"] = "Dani";


Step 3: When you need valu from the session write SessionVariables and dot operator.

Label1.Text = Session[SessionVariables.UserName].ToString();

// Label1.Text = Session["UserName"].ToString();


Result:


Tags

asp.net

session sessionvariables session-variables