Referencing a parent control from a child?

Discussion in 'ASP .Net' started by Steve Hershoff, Jul 30, 2006.

  1. 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, Jul 30, 2006
    #1
    1. Advertising

  2. Steve Hershoff

    Jens Hofmann Guest

    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
    ....
    Jens Hofmann, Jul 30, 2006
    #2
    1. Advertising

  3. 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.
    >
    >
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Jul 31, 2006
    #3
  4. 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.
    >
    >
    >
    =?Utf-8?B?RGF2aWQgSmVzc2Vl?=, Jul 31, 2006
    #4
  5. This is great-- just what I was looking for. Thanks very much, and thanks
    to everyone who replied!

    -Steve


    "David Jessee" <> wrote in message
    news:...
    > 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, Jul 31, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Rodriguez
    Replies:
    23
    Views:
    1,116
    David Schwartz
    Dec 9, 2003
  2. Steve Hershoff

    Referencing a parent control from a child?

    Steve Hershoff, Jul 30, 2006, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    138
    Steve Hershoff
    Jul 31, 2006
  3. Steve Hershoff

    Referencing a parent control from a child?

    Steve Hershoff, Jul 30, 2006, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    166
    Steve Hershoff
    Jul 31, 2006
  4. Noel Dolan
    Replies:
    0
    Views:
    227
    Noel Dolan
    Jul 18, 2004
  5. Bitswapper
    Replies:
    5
    Views:
    130
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page