Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 4th, 2007, 04:25 PM
rlb rlb is offline
Authorized User
Join Date: Jan 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Session Variables in C# Classes

I've discovered a small problem with my C# classes.
I could not seem to access any of my session variables. Tried a few work arounds to no avail.
Finally, I discovered that if I set a dummy session variable first, that I could access all of my session variables.

//can i access my variable
Object i = Session["myvaristhree"];
Response.Write(i);                 //OUTPUT=''

//how about now?
Session["idontmatter"] = 9999999;
i = Session["myvaristhree"];
Response.Write(i);                //OUTPUT='3'
I have my C# class inheriting the System.Web.UI.Page so I can use Session and Response.
This is weird, what's going on?
Old April 5th, 2007, 12:48 AM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

You say "I have my C# class inheriting the System.Web.UI.Page". That statement makes me think that you are trying to use a class derived from System.Web.UI.Page as a utility class. Is this the case? If so, please read on for the explanation as to why you are experiencing problems.

While you can *technically* create a class derived from the ASP.NET base class, you are not going to get the behavior that you expect. The class is derived from the page class, but it's not going to behave like a regular ASP.NET page will when you use it (that is, when the page is loaded by the web server).

A page class in a web application (the class that drives the *.ASPX page) is loaded by the ASP.NET process and many things happen to it. One of those things is the loading of the page's session information. So while your class (derived from the Page class) HAS a session object on it, it's not going to contain anything like you would expect.

If you want to create a utility class that uses page session information, I'd recommend adding a class scope Session class instance, and passing the active page session into it when you create an instance of the utility class. Then you can use the session data in the utility class as you would from the page class code.

An alternative is to use the current HttpContext instance. "System.Web.HttpContext.Current" will give you the http execution context of the current page request. Off of that you can use the ".Session" property.

Old April 5th, 2007, 12:35 PM
rlb rlb is offline
Authorized User
Join Date: Jan 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts

Hmmmmm , thanks for the explanation.

System.Web.HttpContext.Current is one of the things that I had tried and was unsuccessful with, it never seemed to work. Does the HttpContext need to be set somewhere so it will work - or can you give a link for some good reading?
Old April 5th, 2007, 02:15 PM
Wrox Author
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons

No. HttpContext.Current.Server.[property] will work anywhere in a class. I use this method inside some of my helper classes in some of my webapps when I need to map a directory inside the Class as opposed to somewhere in the code behind file.

Show the context that you used HttpContext in so we can see if it is a problem with your code.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
================================================== =========
Why can't Programmers, program??
================================================== =========

Similar Threads
Thread Thread Starter Forum Replies Last Post
Session Variables in C# shikha09 C# 1 November 28th, 2006 10:38 AM
Session Variables testsubject Visual Studio 2005 8 March 8th, 2006 04:26 PM
Get session values from a classes minhpx General .NET 3 August 9th, 2004 10:05 AM
Session Variables flesh_god Dreamweaver (all versions) 7 November 11th, 2003 05:52 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.