Referencing a parent control from a child?

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default Re: Referencing a parent control from a child?

    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 Guest

  4. #3

    Default 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

  5. #4

    Default 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

  6. #5

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139