Ask a Question related to ASP.NET Building Controls, Design and Development.
-
Steve Hershoff #1
Referencing a parent control from a child?
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.
Steve Hershoff Guest
-
Referencing Page properties from child control
I'm declaring public properties in a Page's code behind file (not declaratively). I would like to read and assign these from a child control's cs... -
Why do property changes in child controls get copied to the parent control?
I have a control that contains a collection of another control. When I make a change in a property of one of the members of the collection (using... -
Parent/Child relations - Trying to access child control for save
I have a parent datagrid that has my customer information. For each customer I have a child datagrid with all their part information. In the... -
Notify child control of events in parent control
I have a parent control that has a imagebutton on it, with code to handle the click event. I also have various ascx controls that are loaded by the... -
Child user control accessing parent properties
We have a user control (Titlebar) that loads other user controls (children) into itself based on a property set in the HTML: <fss:Titlebar... -
Jens Hofmann #2
Re: Referencing a parent control from a child?
Steve Hershoff schrieb:
Hello,> 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.
>
>
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
....
Jens Hofmann Guest
-
Peter Bromberg [C# MVP] #3
RE: Referencing a parent control from a child?
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:
[url]http://www.eggheadcafe.com[/url]
UnBlog:
[url]http://petesbloggerama.blogspot.com[/url]
"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.
>
>
>Peter Bromberg [C# MVP] Guest
-
David Jessee #4
RE: Referencing a parent control from a child?
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.
>
>
>David Jessee Guest
-
Steve Hershoff #5
Re: Referencing a parent control from a child?
This is great-- just what I was looking for. Thanks very much, and thanks
to everyone who replied!
-Steve
"David Jessee" <DavidJessee@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;
>
>
Steve Hershoff Guest



Reply With Quote

