![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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. |
#3
| |||
| |||
|
|
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. |
#4
| |||
| |||
|
|
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. |
#5
| |||
| |||
|
|
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; |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |