locked
Using Session State to Pass Value Between Browser Tabs RRS feed

  • Question

  • User217211964 posted

    I have a requirement to maintain a value between browser tabs for the duration of the browser session and cannot use cookies. I thought I could use a session variable and that worked going from the session I established it in to the second session. But when I went from the second tab to the third it no longer existed. It's a bool, so in the first tab I set it to true and when I come into the second tab it was still true, but when I went from the second tab into the third tab surprisingly it was false. 

    Is there another way to maintain state across tabs in a browser session? Am I doing something wrong?

    Here's the definition of my variable:

            protected bool acceptedTerms
            {
                get
                {
                    // Initialize if not already
                    // created
                    if (Session["acceptedTerms"] == null)
                    {
                        Session["acceptedTerms"] = false;
                    }
                    return (bool)Session["acceptedTerms"];
                }
                set
                {
                    Session["acceptedTerms"] = value;
                }
            }

    Within the code I only set it to true in one place otherwise it should be picking up the value from the session. other than the first time in.

    Thank you

    Monday, November 30, 2015 12:38 PM

Answers

  • User281315223 posted

    Session is handled at the application level (and not the client) on a per-user basis, so if you store it within a single tab and open up another one, you should be able to access it within N number of tabs that you open after that. The only reason that I can think that it wouldn't work would be if something was explicitly clearing out your Session. 

    You are setting these values prior to opening other tabs correct?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 30, 2015 2:17 PM

All replies

  • User281315223 posted

    Session is handled at the application level (and not the client) on a per-user basis, so if you store it within a single tab and open up another one, you should be able to access it within N number of tabs that you open after that. The only reason that I can think that it wouldn't work would be if something was explicitly clearing out your Session. 

    You are setting these values prior to opening other tabs correct?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 30, 2015 2:17 PM
  • User217211964 posted

    Thanks,

    After some more testing this morning I sort of realized my problem and I'm still not sure I understand the behavior I'm seeing. I think of our application as a single website because it all run under a single "umbrella" site, but in reality it is made up of 4 different applications. When I said I was opening a new tab, it was actually one of the other applications. What I find so strange is that the first application actually did get my session variable. I can't figure that out, but I tested this several times and it was getting it. 

    I am still trying to figure out a way to pass a value between the applications without having to use a cookie or some other physical means.

    Rodney

    Monday, November 30, 2015 2:50 PM
  • User281315223 posted

    Cookies are generally great for this type of behavior (most sites use them all the time to enable associations or to pass data along between one or more sites). Another option would be to use URL QueryString parameters, but in my opinion that can get rather nasty and cumbersome. Out of those two options, cookies are going to be my recommendation.

    It's possible to actually share a single Session across multiple applications similar to the approach discussed in this related thread (or simply search for "Sharing Session across multiple ASP.NET Applications" in your favorite browser), which could be an option for you as well.

    Monday, November 30, 2015 3:29 PM