Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Referencing a parent control from a child?

Reply
Thread Tools

Referencing a parent control from a child?

 
 
Steve Hershoff
Guest
Posts: n/a
 
      07-30-2006
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
 
 
 
 
Jens Hofmann
Guest
Posts: n/a
 
      07-30-2006
Steve Hershoff schrieb:
> 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
 
 
 
 
Peter Bromberg [C# MVP]
Guest
Posts: n/a
 
      07-30-2006
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:

> 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
 
David Jessee
Guest
Posts: n/a
 
      07-31-2006
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:

> 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
 
Steve Hershoff
Guest
Posts: n/a
 
      07-31-2006
This is great-- just what I was looking for. Thanks very much, and thanks
to everyone who replied!

-Steve


"David Jessee" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
Referencing a parent control from a child? Steve Hershoff ASP .Net Web Controls 4 07-31-2006 05:25 PM
Referencing a parent control from a child? Steve Hershoff ASP .Net 4 07-31-2006 05:25 PM
Page load of the parent page called twice when a modal dialog is opened from a button click of the user control on the parent page Samy ASP .Net 2 08-15-2005 03:30 PM
Referencing parent page controls John Spiegel ASP .Net 4 01-04-2005 01:33 PM



Advertisments