Control and Parent

Discussion in 'ASP .Net Building Controls' started by Zürcher See, Nov 5, 2004.

  1. Zürcher See

    Zürcher See Guest

    The Control class has the Parent property that is readonly.
    When the control is added to a controls collection of another control the
    Parent property refers to that control.
    "Who" set the Parent property? How to implement this "mechanism"?

    public abstract class MyControl
    {
    private MyControl parent;
    public MyControl Parent{get{return this.parent;}}

    private MyControlCollection controls;
    public MyControlCollection Controls{get{return controls;}}
    }

    public class MyControlCollection : ArraList
    {
    protected new int Add(object value){return base.Add(value);}
    public virtual Add(MyControl control)
    {
    //I think here schould be set the parent property, but how???
    return this.Add(control);
    }
    }

    Any help will be appreciated , thanks
     
    Zürcher See, Nov 5, 2004
    #1
    1. Advertising

  2. Zürcher, you can use Reflector [http://www.aisto.com/roeder/dotnet/] to
    view the source code of the .NET Framework BCL. If you look at the
    System.Web.UI.Control class you'll see that it has an AddedControl()
    method that is called from the ControlCollection class when the .Add()
    method is called.

    In AddedControl(), the code looks like:

    protected internal virtual void AddedControl(Control control, int index)
    {
    if (control._parent != null)
    {
    control._parent.Controls.Remove(control);
    }
    control._parent = this;

    ...
    }


    As you can see, it takes in the Control to add as the first parameter,
    and immediately sets the added control's private _parent property to
    itself (namely, control._parent = this;)

    hth

    --

    Scott Mitchell

    http://www.4GuysFromRolla.com

    * When you think ASP.NET, think 4GuysFromRolla.com!
     
    Scott Mitchell [MVP], Nov 5, 2004
    #2
    1. Advertising

  3. Zürcher See

    Zürcher See Guest

    Yes, I have noticed the AddedControl method, but is protected, the
    ControlCollection.Add method can't call it! Or I missing something?

    "Scott Mitchell [MVP]" <> schrieb im Newsbeitrag
    news:...
    > Zürcher, you can use Reflector [http://www.aisto.com/roeder/dotnet/] to
    > view the source code of the .NET Framework BCL. If you look at the
    > System.Web.UI.Control class you'll see that it has an AddedControl()
    > method that is called from the ControlCollection class when the .Add()
    > method is called.
    >
    > In AddedControl(), the code looks like:
    >
    > protected internal virtual void AddedControl(Control control, int index)
    > {
    > if (control._parent != null)
    > {
    > control._parent.Controls.Remove(control);
    > }
    > control._parent = this;
    >
    > ...
    > }
    >
    >
    > As you can see, it takes in the Control to add as the first parameter, and
    > immediately sets the added control's private _parent property to itself
    > (namely, control._parent = this;)
    >
    > hth
    >
    > --
    >
    > Scott Mitchell
    >
    > http://www.4GuysFromRolla.com
    >
    > * When you think ASP.NET, think 4GuysFromRolla.com!
     
    Zürcher See, Nov 5, 2004
    #3
  4. To implement this yourself in a class, the class will likely have to
    extend the System.Web.UI.Control class.

    If you absolutely must twiddle with this private member variable
    (_parent), you can do so using Reflection, but that will add a perf. hit
    at runtime - see http://tinyurl.com/49z4n for info on setting a private
    property using Reflection; see http://tinyurl.com/5nvr4 for a discussion
    on the "cost" of Reflection.

    hth


    Zürcher See wrote:
    > Yes, I have noticed the AddedControl method, but is protected, the
    > ControlCollection.Add method can't call it! Or I missing something?
    >
    > "Scott Mitchell [MVP]" <> schrieb im Newsbeitrag
    > news:...
    >
    >>Zürcher, you can use Reflector [http://www.aisto.com/roeder/dotnet/] to
    >>view the source code of the .NET Framework BCL. If you look at the
    >>System.Web.UI.Control class you'll see that it has an AddedControl()
    >>method that is called from the ControlCollection class when the .Add()
    >>method is called.
    >>
    >>In AddedControl(), the code looks like:
    >>
    >>protected internal virtual void AddedControl(Control control, int index)
    >>{
    >> if (control._parent != null)
    >> {
    >> control._parent.Controls.Remove(control);
    >> }
    >> control._parent = this;
    >>
    >> ...
    >>}
    >>
    >>
    >>As you can see, it takes in the Control to add as the first parameter, and
    >>immediately sets the added control's private _parent property to itself
    >>(namely, control._parent = this;)
    >>
    >>hth
    >>
    >>--
    >>
    >> Scott Mitchell
    >>
    >> http://www.4GuysFromRolla.com
    >>
    >>* When you think ASP.NET, think 4GuysFromRolla.com!

    >
    >
    >



    --

    Scott Mitchell

    http://www.4GuysFromRolla.com

    * When you think ASP.NET, think 4GuysFromRolla.com!
     
    Scott Mitchell [MVP], Nov 5, 2004
    #4
    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. Roy
    Replies:
    2
    Views:
    3,121
  2. mark
    Replies:
    1
    Views:
    575
  3. metal
    Replies:
    8
    Views:
    508
    Bruno Desthuilliers
    Oct 30, 2009
  4. Bill S.
    Replies:
    2
    Views:
    216
    Bill S.
    May 8, 2004
  5. PD
    Replies:
    6
    Views:
    716
Loading...

Share This Page