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

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

Post by Chad Schar » Thu, 18 Oct 2007 04:54:16


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[i]);

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[i]);"
the Count is 0, however after I add the label it's 1 and the extender it's
2.

I'm stumped.

Thanks,
Chad
 
 
 

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

Post by IfThenEls » Thu, 18 Oct 2007 07:48:27

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

 
 
 

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

Post by Chad Schar » Fri, 19 Oct 2007 03:33:33

_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.
 
 
 

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

Post by Chad Schar » Sat, 20 Oct 2007 00:53:33

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