locked
Using Application and Session variables in ASP.NET and C# Web Sites RRS feed

  • Question

  • User569238728 posted

    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?

    Thanks,

    Josh

    Sunday, September 15, 2019 3:32 AM

Answers

  • User665608656 posted

    Hi ASP_User_qwert,

    I guess what I was wondering is can the JavaScript use the application and session variables of the asp.net / C#.

    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 :

     <script type="text/javascript">
    function SetUserName()
    {
        var userName = "Shekhar Shete";
        '<%Session["UserName"] = "' + userName + '"; %>';
         alert('<%=Session["UserName"] %>');
    }
    </script>

    Using session in js, you can refer to the following:

    <script type="text/javascript">
        function GetUserName()
        {
    
            var username = '<%= Session["UserName"] %>';
            alert(username );
        }
    </script>

    For more details, you could refer to this link: https://stackoverflow.com/a/20236481

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 23, 2019 1:32 AM

All replies

  • User-158764254 posted

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

    https://docs.microsoft.com/en-us/dotnet/api/system.web.httpcontext.current?view=netframework-4.8

    Sunday, September 15, 2019 1:25 PM
  • User665608656 posted

    Hi ASP_User_qwert,

    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 variables.

    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 this
    HttpContext.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 this
    Session["a"]="test";

    Best Regards,
    YongQing.

    Monday, September 16, 2019 8:59 AM
  • User569238728 posted

    I guess what I was wondering is can the JavaScript use the application and session variables of the asp.net / C#.

    Sorry for being late, I didn't know that I am supposed to choose the right answer!  :) 

    Sunday, September 22, 2019 11:12 PM
  • User665608656 posted

    Hi ASP_User_qwert,

    I guess what I was wondering is can the JavaScript use the application and session variables of the asp.net / C#.

    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 :

     <script type="text/javascript">
    function SetUserName()
    {
        var userName = "Shekhar Shete";
        '<%Session["UserName"] = "' + userName + '"; %>';
         alert('<%=Session["UserName"] %>');
    }
    </script>

    Using session in js, you can refer to the following:

    <script type="text/javascript">
        function GetUserName()
        {
    
            var username = '<%= Session["UserName"] %>';
            alert(username );
        }
    </script>

    For more details, you could refer to this link: https://stackoverflow.com/a/20236481

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 23, 2019 1:32 AM