HighTechTalks DotNet Forums  

Referencing a parent control from a child?

ASP.net Building Controls microsoft.public.dotnet.framework.aspnet.buildingcontrols


Discuss Referencing a parent control from a child? in the ASP.net Building Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Steve Hershoff
 
Posts: n/a

Default Referencing a parent control from a child? - 07-30-2006 , 05:09 PM






I have two UserControls I'd like to have talk to each other. One of them is
contained within the other, in a parent/child relationship. The child
control is loaded dynamically (it's declared as a WebControls.Placeholder)
in the parent through a call to LoadControl().

I'm using events to communicate between them. The parent will fire an
event, and if the child is listening it will do something. That's the idea
at least.


In practice I'm having trouble getting the parent's reference while in the
child. My code looks like this:

public abstract class MyChildControl : System.Web.UI.UserControl {
.....
protected MyParentControl parent;
.....
parent = (MyParentControl) this.Parent; //big blowup here
.....
}

I'm getting an invalid cast error when I call the last line. Obviously I'm
not clear on how to go about getting a reference to the parent from within
the child. Could someone show me the proper way of doing this? Thanks very
much.



Reply With Quote
  #2  
Old   
Jens Hofmann
 
Posts: n/a

Default Re: Referencing a parent control from a child? - 07-30-2006 , 05:45 PM






Steve Hershoff schrieb:
Quote:
I have two UserControls I'd like to have talk to each other. One of them is
contained within the other, in a parent/child relationship. The child
control is loaded dynamically (it's declared as a WebControls.Placeholder)
in the parent through a call to LoadControl().

I'm using events to communicate between them. The parent will fire an
event, and if the child is listening it will do something. That's the idea
at least.


In practice I'm having trouble getting the parent's reference while in the
child. My code looks like this:

public abstract class MyChildControl : System.Web.UI.UserControl {
.....
protected MyParentControl parent;
.....
parent = (MyParentControl) this.Parent; //big blowup here
.....
}

I'm getting an invalid cast error when I call the last line. Obviously I'm
not clear on how to go about getting a reference to the parent from within
the child. Could someone show me the proper way of doing this? Thanks very
much.


Hello,

first I would set a breakpoint to this line and check what type of
object this.Parent is. Maybe it's a Panel .. or some other Container...

....
parent = (MyParentControl) this.Parent
....


Reply With Quote
  #3  
Old   
Peter Bromberg [C# MVP]
 
Posts: n/a

Default RE: Referencing a parent control from a child? - 07-30-2006 , 06:57 PM



If it doesn't absolutely "have to be" abstract, that's another area that
could cause this problem.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




"Steve Hershoff" wrote:

Quote:
I have two UserControls I'd like to have talk to each other. One of them is
contained within the other, in a parent/child relationship. The child
control is loaded dynamically (it's declared as a WebControls.Placeholder)
in the parent through a call to LoadControl().

I'm using events to communicate between them. The parent will fire an
event, and if the child is listening it will do something. That's the idea
at least.


In practice I'm having trouble getting the parent's reference while in the
child. My code looks like this:

public abstract class MyChildControl : System.Web.UI.UserControl {
.....
protected MyParentControl parent;
.....
parent = (MyParentControl) this.Parent; //big blowup here
.....
}

I'm getting an invalid cast error when I call the last line. Obviously I'm
not clear on how to go about getting a reference to the parent from within
the child. Could someone show me the proper way of doing this? Thanks very
much.




Reply With Quote
  #4  
Old   
David Jessee
 
Posts: n/a

Default RE: Referencing a parent control from a child? - 07-31-2006 , 09:29 AM



Easiest way I can think of is:

ParentControlType theParent=null;
Control tempControl=this;
while((!tempControl is ParentControlType)&&(!tempControl is Page))
tempControl=tempControl.Parent;
if(tempControl is ParentControlType)
theParent = (ParentControlType)tempControl;




"Steve Hershoff" wrote:

Quote:
I have two UserControls I'd like to have talk to each other. One of them is
contained within the other, in a parent/child relationship. The child
control is loaded dynamically (it's declared as a WebControls.Placeholder)
in the parent through a call to LoadControl().

I'm using events to communicate between them. The parent will fire an
event, and if the child is listening it will do something. That's the idea
at least.


In practice I'm having trouble getting the parent's reference while in the
child. My code looks like this:

public abstract class MyChildControl : System.Web.UI.UserControl {
.....
protected MyParentControl parent;
.....
parent = (MyParentControl) this.Parent; //big blowup here
.....
}

I'm getting an invalid cast error when I call the last line. Obviously I'm
not clear on how to go about getting a reference to the parent from within
the child. Could someone show me the proper way of doing this? Thanks very
much.




Reply With Quote
  #5  
Old   
Steve Hershoff
 
Posts: n/a

Default Re: Referencing a parent control from a child? - 07-31-2006 , 12:25 PM



This is great-- just what I was looking for. Thanks very much, and thanks
to everyone who replied!

-Steve


"David Jessee" <DavidJessee (AT) discussions (DOT) microsoft.com> wrote

Quote:
Easiest way I can think of is:

ParentControlType theParent=null;
Control tempControl=this;
while((!tempControl is ParentControlType)&&(!tempControl is Page))
tempControl=tempControl.Parent;
if(tempControl is ParentControlType)
theParent = (ParentControlType)tempControl;





Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.