|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Referencing a parent control from a child?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. Steve Hershoff schrieb:
Show quoteHide quote > I have two UserControls I'd like to have talk to each other. One of them is Hello,> 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. > > 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 .... If it doesn't absolutely "have to be" abstract, that's another area that
could cause this problem. Peter -- Show quoteHide quoteCo-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. > > > 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; Show quoteHide quote "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. > > > This is great-- just what I was looking for. Thanks very much, and thanks
to everyone who replied! -Steve Show quoteHide quote "David Jessee" <DavidJes***@discussions.microsoft.com> wrote in message news:1B31F947-6F99-4C05-8031-C692E07A552E@microsoft.com... > 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; > >
disable an ascx entirely in my web page
I guess a simple question but I cant find it :-( Integration of GridView and FormView DropDownList skin - assign BorderColor format text in dropdownlist Allowing entry of a Carriage Return during data entry DropdownLisr problem on postback Change report chart type ad runtime. dropdownlist multiple fields in text? difference between user control and server control |
|||||||||||||||||||||||