I am instantiating an object in an ASP.NET page. From this object I want to
be able to access Session. I dont want to change the constructor to pass the
page reference to it, and I dont want to have to add extra properties.
How can I get a reference to the page object in which the object is
instantiated.
Cheers
> I am instantiating an object in an ASP.NET page. From this object I want to
> be able to access Session. I dont want to change the constructor to pass the
> page reference to it, and I dont want to have to add extra properties.
>
> How can I get a reference to the page object in which the object is
> instantiated.
Why would you believe there's a way to do this? Objects don't have the
notion of a "creator". You need to tell them about objects.
It's possible that there's some thread-static way to get to the
"current page" via HttpContext.Current.Request or something similar...
but there's no general solution.
If you want to give access to the reference, why don't you want to
change the constructor? You're really trying to add state without
adding state...

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Microsoft Newsserver - 28 Feb 2008 15:37 GMT
>> I am instantiating an object in an ASP.NET page. From this object I want
>> to
[quoted text clipped - 7 lines]
> Why would you believe there's a way to do this? Objects don't have the
> notion of a "creator". You need to tell them about objects.
Answer.
It was a question. I was wondering if there was a way do do this using
reflection or some other method.
A simple yes or no would have sufficed.
> It's possible that there's some thread-static way to get to the
> "current page" via HttpContext.Current.Request or something similar...
> but there's no general solution.
Answer.
Actually, I had forgotten about the system.web namespace which I added
as a reference and then was able to acheive this.
> If you want to give access to the reference, why don't you want to
> change the constructor? You're really trying to add state without
> adding state...
No comment.
Hello Microsoft,
> I am instantiating an object in an ASP.NET page. From this object I
> want to be able to access Session. I dont want to change the
[quoted text clipped - 3 lines]
> How can I get a reference to the page object in which the object is
> instantiated.
You can use the HttpContext.Current static variable. You can access both
the current page and the session from there.
--
Jesse Houwing
jesse.houwing at sogeti.n
Microsoft Newsserver - 28 Feb 2008 15:38 GMT
Thanks for your help.
I added the system.web namspace and used it. I had forgotten about that
facility.
> Hello Microsoft,
>
[quoted text clipped - 12 lines]
> Jesse Houwing
> jesse.houwing at sogeti.nl
Arne Vajhøj - 29 Feb 2008 04:05 GMT
>> I am instantiating an object in an ASP.NET page. From this object I
>> want to be able to access Session. I dont want to change the
[quoted text clipped - 6 lines]
> You can use the HttpContext.Current static variable. You can access both
> the current page and the session from there.
The class will only work in web context then.
I would try and avoid that type of coupling.
Arne
Jesse Houwing - 29 Feb 2008 10:03 GMT
Hello Arne,
>>> I am instantiating an object in an ASP.NET page. From this object I
>>> want to be able to access Session. I dont want to change the
[quoted text clipped - 10 lines]
>
> I would try and avoid that type of coupling.
You could check
If (HttpContext.Current != null)
{
// In web context
}
else
{
// other
}
Either way, if you need the Session or elements on the page in the class,
you're coupled any way.
--
Jesse Houwing
jesse.houwing at sogeti.n
Arne Vajhøj - 29 Feb 2008 17:43 GMT
>>>> I am instantiating an object in an ASP.NET page. From this object I
>>>> want to be able to access Session. I dont want to change the
[quoted text clipped - 24 lines]
> Either way, if you need the Session or elements on the page in the
> class, you're coupled any way.
Yes.
The problem is not the answer but the question.
Arne
Jesse Houwing - 29 Feb 2008 18:04 GMT
Hello Arne,
>>>>> I am instantiating an object in an ASP.NET page. From this object
>>>>> I want to be able to access Session. I dont want to change the
[quoted text clipped - 27 lines]
>
> The problem is not the answer but the question.
Yes, but that also depends on what the class must do. If it's specialised
in formattign or chackign controls on the page, then this is a completely
logocal thing to do...
--
Jesse Houwing
jesse.houwing at sogeti.nl
Arne Vajhøj - 29 Feb 2008 18:40 GMT
>>>>>> I am instantiating an object in an ASP.NET page. From this object
>>>>>> I want to be able to access Session. I dont want to change the
[quoted text clipped - 31 lines]
> specialised in formattign or chackign controls on the page, then this is
> a completely logocal thing to do...
But making the Session or whatever being transferred as an argument
instead of grabbed via .Current would make the dependency explicit.
Arne
Yes, you can do this, as Jesse has mentioned, by working through current
context.
There is also a good reason why this, as a rule, is not a good idea. You are
tightly coupling your class (or class library) to a web application. This
does a couple of things:
1. Makes it difficult, if not impossible, to unit test using unit testing
frameworks, as you have forced a dependency that cannot easily be injected,
if at all
2. Forces a complete redesign of your application if you ever move to
another type of library (web service, smart client, etc.)
You are best advised to grab the info you need off the page before you fire
off the library, as this leaves no circular dependencies.

Signature
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA
*************************************************
| Think outside the box!
*************************************************
>I am instantiating an object in an ASP.NET page. From this object I want to
>be able to access Session. I dont want to change the constructor to pass
[quoted text clipped - 4 lines]
>
> Cheers
Microsoft Newsserver - 29 Feb 2008 08:24 GMT
Point noted. - Thanks
> Yes, you can do this, as Jesse has mentioned, by working through current
> context.
[quoted text clipped - 24 lines]
>>
>> Cheers