Last post Sep 23, 2019 01:32 AM by Yongqing Yu
Sep 15, 2019 03:32 AM|ASP_User_qwert|LINK
I'm trying to use Session and Application variables in my ASP.NET, C# functions. Could someone please let me know what options I have, is this possible? Here's some code that gives me red squiggles.
public class MyClass
public void MyFunction()
Application["a"] = "test";
Also, may they be passed in?
Sep 15, 2019 01:25 PM|mbanavige|LINK
While a page class has access to things like Application and Session, a basic class does not.
You could use HttpContext from your basic class to gain access though:
HttpContext.Current.Application["a"] = "test";
Sep 16, 2019 08:59 AM|Yongqing Yu|LINK
Session and Application are very important in ASP.NET. When we move from one page to another page, the values of the previous page will get lost, If we want to hold the previous values, for that purpose we can use session level variable or application level
More about Session and Application in asp.net, you can refer to this link : Session and Application in ASP.NET
As mbanavige said, in class, if you want to use Application, you should use it like this :
HttpContext.Current.Application["a"] = "test";//or use session like thisHttpContext.Current.Session["a"] = "test";
When you are setting the value in code behind file of a page, you can simply use:
Application["a"]="test";//or use session like thisSession["a"]="test";
Sep 22, 2019 11:12 PM|ASP_User_qwert|LINK
Sorry for being late, I didn't know that I am supposed to choose the right answer! :)
Sep 23, 2019 01:32 AM|Yongqing Yu|LINK
Your previous post did not mention using session or application in js.
But if you want to use session in js, first you need to create a session variable that can be used in your code behind, as we said earlier or create it in js like below :
var userName = "Shekhar Shete";
'<%Session["UserName"] = "' + userName + '"; %>';
Using session in js, you can refer to the following:
var username = '<%= Session["UserName"] %>';
For more details, you could refer to this link: https://stackoverflow.com/a/20236481