Saturday, March 24, 2012

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

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,
ChadWhat is the value of _menus.Count before the for loop

"Chad Scharf" <chadscharf@.community.nospamwrote in message
news:%23qrSW5CEIHA.5752@.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

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
>


_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" <sql_agentman@.hotmail.comwrote in message
news:%23D0oraEEIHA.6120@.TK2MSFTNGP05.phx.gbl...

Quote:

Originally Posted by

What is the value of _menus.Count before the for loop
>
>
"Chad Scharf" <chadscharf@.community.nospamwrote in message
news:%23qrSW5CEIHA.5752@.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

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


>
>


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" <chadscharf@.community.nospamwrote in message
news:%23qrSW5CEIHA.5752@.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

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
>

0 comments:

Post a Comment