views:

11

answers:

1

I have an ASP.NET web form that has a "container" usercontrol that hosts several custom user controls on the page. The controls can be hosted directly in the container or can be children of other usercontrols. The container usercontrol has several public properties exposed that I sometimes need to get to from within the child user controls. I've been using some form of "this.Parent" or "this.Parent.Parent" to get back to the base control.

What would be the impact of storing a reference to "this" into Session from the base control so I can access it from within the event handlers within the user controls?

Thanks,
Darvis

A: 

You can put this in your page:

public MyControlType MyParentControl
{
   get
   {
       return this._myParentUserControl;
   }
}

And put this in your Custom Controls:

var parentPage = this.Page as MyBasePageType;
if(parentPage !=null){
    parentPage.MyParentControl.WhateverFuncNameYouNeed("myParams");
}
Pete Amundson
you'll get +1 brownie points for casting to an Interface instead of a concrete type
Pete Amundson
Thanks, that would do the trick if it weren't for the fact that my question was worded very poorly and didn't really explain what I'm trying to do. I clarified in a comment.
Darvis Lombardo
ah...well the same trick will work (with tweaking) I would really really suggest never putting references to user controls in session...i'll update my answer
Pete Amundson
I also edited the original post to better reflect my problem.
Darvis Lombardo
"I would really really suggest never putting references to user controls in session"...I would say the same thing, but I can't give a good reason why. When I retrieve the reference from session and then cast it back to its original type, does a completely new instance of the class get created or just the reference? If it's just the reference then I'm not sure I understand why it would be such a bad thing.
Darvis Lombardo
Just like any other object, it can be stored within "InProc" session. However it will exist as long as the session exists which isn't really what you need. You will also have to be careful that the object always is updated to the current version (because the last request's version will exist in session and won't be attached to the page). It just isn't a good practice and can have consequences if not carefully controlled.
Pete Amundson