this.Controls.Add(Control) Does Not Add Control to child collection (this.Controls.Count == 0)

Discussion in 'ASP .Net' started by Chad Scharf, Oct 16, 2007.

  1. Chad Scharf

    Chad Scharf Guest

    Ok, as silly as it may sound, I have a situation where I am creating a
    CompositeControl in ASP.NET 2.0, C#. I have the following code in the
    CreateChildControls() method that build the control's child control
    collection:

    for (int i = 0; i < _menus.Count; i++)
    this.Controls.Add(_menus);

    foreach (FloatingMenu menu in _menus)
    {
    Label lbl = new Label();
    lbl.ID = string.Concat("GroupQ", menu.ID);
    lbl.CssClass = "menuItemGroupHeader";
    lbl.Text = menu.GroupName;
    this.Controls.Add(lbl);

    HoverMenuExtender extender = new HoverMenuExtender();
    extender.ID = string.Concat("ExtenderQ", menu.ID);
    extender.TargetControlID = lbl.ID;
    extender.PopupControlID = menu.ID;
    extender.PopupPosition = HoverMenuPopupPosition.Bottom;
    this.Controls.Add(extender);
    }


    The amazing thing is here, that the Label control and the HoverMenuExtender
    AJAX Toolkit control both get added just fine, however I added a watch on
    this.Controls.Count for the line where I add my FloatingMenu control
    collection, "_menus", and after each call to "this.Controls.Add(_menus);"
    the Count is 0, however after I add the label it's 1 and the extender it's
    2.

    I'm stumped.

    Thanks,
    Chad
     
    Chad Scharf, Oct 16, 2007
    #1
    1. Advertising

  2. Chad Scharf

    IfThenElse Guest

    What is the value of _menus.Count before the for loop


    "Chad Scharf" <> wrote in message
    news:%...
    > Ok, as silly as it may sound, I have a situation where I am creating a
    > CompositeControl in ASP.NET 2.0, C#. I have the following code in the
    > CreateChildControls() method that build the control's child control
    > collection:
    >
    > for (int i = 0; i < _menus.Count; i++)
    > this.Controls.Add(_menus);
    >
    > foreach (FloatingMenu menu in _menus)
    > {
    > Label lbl = new Label();
    > lbl.ID = string.Concat("GroupQ", menu.ID);
    > lbl.CssClass = "menuItemGroupHeader";
    > lbl.Text = menu.GroupName;
    > this.Controls.Add(lbl);
    >
    > HoverMenuExtender extender = new HoverMenuExtender();
    > extender.ID = string.Concat("ExtenderQ", menu.ID);
    > extender.TargetControlID = lbl.ID;
    > extender.PopupControlID = menu.ID;
    > extender.PopupPosition = HoverMenuPopupPosition.Bottom;
    > this.Controls.Add(extender);
    > }
    >
    >
    > The amazing thing is here, that the Label control and the
    > HoverMenuExtender AJAX Toolkit control both get added just fine, however I
    > added a watch on this.Controls.Count for the line where I add my
    > FloatingMenu control collection, "_menus", and after each call to
    > "this.Controls.Add(_menus);" the Count is 0, however after I add the
    > label it's 1 and the extender it's 2.
    >
    > I'm stumped.
    >
    > Thanks,
    > Chad
    >
     
    IfThenElse, Oct 16, 2007
    #2
    1. Advertising

  3. Chad Scharf

    Chad Scharf Guest

    _menus.Count is 2 before the loop. And the second, foreach loop adds 2
    Labels and 2 HoverMenuExtenders as it should. I tried the first loop with a
    foreach initially, but remembering SyncRoot issues and thought perhaps the
    enumerator was breaking the Add operation, I changed it to the for loop to
    no avail.


    "IfThenElse" <> wrote in message
    news:%...
    > What is the value of _menus.Count before the for loop
    >
    >
    > "Chad Scharf" <> wrote in message
    > news:%...
    >> Ok, as silly as it may sound, I have a situation where I am creating a
    >> CompositeControl in ASP.NET 2.0, C#. I have the following code in the
    >> CreateChildControls() method that build the control's child control
    >> collection:
    >>
    >> for (int i = 0; i < _menus.Count; i++)
    >> this.Controls.Add(_menus);
    >>
    >> foreach (FloatingMenu menu in _menus)
    >> {
    >> Label lbl = new Label();
    >> lbl.ID = string.Concat("GroupQ", menu.ID);
    >> lbl.CssClass = "menuItemGroupHeader";
    >> lbl.Text = menu.GroupName;
    >> this.Controls.Add(lbl);
    >>
    >> HoverMenuExtender extender = new HoverMenuExtender();
    >> extender.ID = string.Concat("ExtenderQ", menu.ID);
    >> extender.TargetControlID = lbl.ID;
    >> extender.PopupControlID = menu.ID;
    >> extender.PopupPosition = HoverMenuPopupPosition.Bottom;
    >> this.Controls.Add(extender);
    >> }
    >>
    >>
    >> The amazing thing is here, that the Label control and the
    >> HoverMenuExtender AJAX Toolkit control both get added just fine, however
    >> I added a watch on this.Controls.Count for the line where I add my
    >> FloatingMenu control collection, "_menus", and after each call to
    >> "this.Controls.Add(_menus);" the Count is 0, however after I add the
    >> label it's 1 and the extender it's 2.
    >>
    >> I'm stumped.
    >>
    >> Thanks,
    >> Chad
    >>

    >
    >
     
    Chad Scharf, Oct 17, 2007
    #3
  4. Chad Scharf

    Chad Scharf Guest

    Does anyone have any clue why this would happen? I'm still banging my head
    against a wall trying to figure this out.

    "Chad Scharf" <> wrote in message
    news:%...
    > Ok, as silly as it may sound, I have a situation where I am creating a
    > CompositeControl in ASP.NET 2.0, C#. I have the following code in the
    > CreateChildControls() method that build the control's child control
    > collection:
    >
    > for (int i = 0; i < _menus.Count; i++)
    > this.Controls.Add(_menus);
    >
    > foreach (FloatingMenu menu in _menus)
    > {
    > Label lbl = new Label();
    > lbl.ID = string.Concat("GroupQ", menu.ID);
    > lbl.CssClass = "menuItemGroupHeader";
    > lbl.Text = menu.GroupName;
    > this.Controls.Add(lbl);
    >
    > HoverMenuExtender extender = new HoverMenuExtender();
    > extender.ID = string.Concat("ExtenderQ", menu.ID);
    > extender.TargetControlID = lbl.ID;
    > extender.PopupControlID = menu.ID;
    > extender.PopupPosition = HoverMenuPopupPosition.Bottom;
    > this.Controls.Add(extender);
    > }
    >
    >
    > The amazing thing is here, that the Label control and the
    > HoverMenuExtender AJAX Toolkit control both get added just fine, however I
    > added a watch on this.Controls.Count for the line where I add my
    > FloatingMenu control collection, "_menus", and after each call to
    > "this.Controls.Add(_menus);" the Count is 0, however after I add the
    > label it's 1 and the extender it's 2.
    >
    > I'm stumped.
    >
    > Thanks,
    > Chad
    >
     
    Chad Scharf, Oct 18, 2007
    #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. Replies:
    3
    Views:
    917
    Natty Gur
    Nov 3, 2003
  2. Harry
    Replies:
    2
    Views:
    662
    Harry
    Apr 16, 2004
  3. Jeff Rodriguez
    Replies:
    23
    Views:
    1,130
    David Schwartz
    Dec 9, 2003
  4. Øyvind Isaksen
    Replies:
    1
    Views:
    979
    Øyvind Isaksen
    May 18, 2007
  5. Replies:
    0
    Views:
    207
Loading...

Share This Page