0

I start in a page that inherits from System.Web.UI.Page. Page.Session is not null at this point. The page creates a new class. This class inherits from another class that inherits from System.Web.UI.UserControl. Once I get into the class, Session is null. What do I need for Session state to carry into the class?

Both the page and class are in the same webapp.

4thSpace
  • 43,672
  • 97
  • 296
  • 475
  • What means "once i get into the class"? The `Session` doesn't need to be _carried_, it's available in the application after the user has visited the first page.(f.e via `HttpContext.Current.Session` as Hanlet has shown) – Tim Schmelter Dec 05 '12 at 22:06
  • You say you create a new class that inherits from UserControl. Is the control on the page? How is it being instantiated? – Brad Patton Dec 05 '12 at 22:09
  • If the control is on the page you should be able to do 'this.Page.Session' as well as 'HttpContext.Current.Session'. – Brad Patton Dec 05 '12 at 22:10

1 Answers1

2

Have you tried using:

HttpContext.Current.Session

?

Good luck!

Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75
  • Works! What is the difference with just Session where I got the null? – 4thSpace Dec 05 '12 at 23:06
  • I think the difference is that HttpContext.Current.Session simply returns null if the session is not available, whereas your other class probably threw an exception. Check this out http://stackoverflow.com/a/6021261/752527 – Hanlet Escaño Dec 05 '12 at 23:13