0

I have an ascx. In this ascx, I have any number of controls that I can interact with and set a session variable and it works as expected.

However, when (in the same control), I try to set session in page load, I get a different error depending on how I implement it:

System.Web.HttpContext.Current.Session("Test") = "Test"

It says that, summarized, the Session object is nothing.

If, instead, I say:

Session("Test") = "Test"

It then tells me that I need to enable session state in either the page or web.config. I have indeed checked and session is enabled in the web.config and furthermore, those session statements that are called in response to a click on a runat="server" control works fine.

At this point, I'm almost certain that my issue is because I lack knowledge on the lifecycle of the page and session object. So I therefore have two questions:

  1. Why isn't this working as I expect? I suspect that I'm trying to call Session before some other code which makes it available is executing but what might that be?

  2. How can I make it work so that I can store information in a session variable on page load (and subsequently clear it on page unload)? If I cannot make this work as I hope, how can I do something similar server-side?

user4593252
  • 3,496
  • 6
  • 29
  • 55
  • That should work, but you could also try the other way since an ascx has a reference to the `Page` which has a reference to the session: `Page.Session(Test") = "Test"` – Tim Schmelter Apr 19 '16 at 14:15
  • It tells me to enable session in page directive or web.config. :( – user4593252 Apr 19 '16 at 14:17
  • you have `enableSessionState="true"` in page directive or web.config? – Tim Schmelter Apr 19 '16 at 14:18
  • This is a page directive: `<%@Page enableSessionState="true">` or via web.config: ` `. Maybe this helps: http://stackoverflow.com/questions/14334147/session-state-can-only-be-used-when-enablesessionstate-is-set-to-true-either-in – Tim Schmelter Apr 19 '16 at 14:20
  • 1. *It's already in the web.config* I just pasted the line for you. 2. It's an ascx. It doesn't know what a page directive is. 3. There are already Session variables in use elsewhere in the page. The problem is that I cannot access Session on page load. – user4593252 Apr 19 '16 at 14:23

1 Answers1

0

The problem was caused by having multiple tabs in the same browser open.

To clarify...

Say maybe tab 1 is open to the page before where I tried to set session because I'm debugging and lazy and didn't close other tabs.

And then maybe tab 2 is the current tab that just opened when I started debugging. The only code change is where I'm trying to set a session variable.

There are now two tabs open with conflicting session state data. When the current code tries to set session, it is in conflict with what is being sent back and forth to "wherever sessions live". Or maybe it doesn't really work that way but closing the old tabs opened with "older code" and having only tabs opened that face the "new code" solves the problem.

Community
  • 1
  • 1
user4593252
  • 3,496
  • 6
  • 29
  • 55
  • In case this could be a problem for the users of your application, I discussed that subject in this post: http://stackoverflow.com/questions/36367189/use-different-session-if-user-logs-in-multiple-times/36367912#36367912. – ConnorsFan Apr 19 '16 at 19:29
  • Interesting solution however, there are two problems I cannot overcome. 1. This is a "partial" that does not have a form and I cannot add forms willy-nilly because this partial is site-wide (aspx). Thus, I cannot rely on view state. As this is a partial, I can't use query strings for embedded data like this. 2. There are no logins for this feature or partial or anywhere on the site, actually – user4593252 Apr 19 '16 at 20:37