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

C

Chad Scharf

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
 
C

Chad Scharf

_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 said:
What is the value of _menus.Count before the for loop


Chad Scharf said:
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

 
C

Chad Scharf

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,153
Latest member
NamKaufman
Top