469,950 Members | 1,855 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

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

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
Oct 16 '07 #1
3 2326
What is the value of _menus.Count before the for loop
"Chad Scharf" <ch********@community.nospamwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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

Oct 16 '07 #2
_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" <sq**********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
What is the value of _menus.Count before the for loop
"Chad Scharf" <ch********@community.nospamwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>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


Oct 17 '07 #3
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" <ch********@community.nospamwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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

Oct 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Bad_Kid | last post: by
1 post views Thread by Shloma Baum | last post: by
3 posts views Thread by 180Reef | last post: by
3 posts views Thread by Charlie Brown | last post: by
reply views Thread by mtczx232 | last post: by
reply views Thread by R.A.F. | last post: by
9 posts views Thread by Mad Cliffy's Legs Don't Work | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.