Controls.Count

  • Thread starter Thread starter Paperback Writer
  • Start date Start date
P

Paperback Writer

Hi everybody!
I tryed to use the Controls.Count in my WebForm to get the number of
controls, but it's always showing 3 Controls!
In the UserControls it works perfectly!!!

Is tha a bug or do I something false ?
 
The Controls collection of a System.Web.UI.Control is a collection of all
*immediate* child Controls in that Control. Controls are containers for
other Controls, and Controls can be nested to many levels. For example, you
can have a Form that contains a Panel with 3 Labels in it. According to the
above scenario, the Form has 1 Control in its Controls Collection. The Panel
contains 3 Controls in its Controls Collection.

BTW, in the future, you'll get better/more answers regarding ASP.Net on the
microsoft.public.dotnet.framwork.aspnet newsgroup.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Big things are made up of
lots of little things.
 
Hi,

It gives you the controls that are under the page itself, if you use
something like a panel to group controls these inner controls are not part
of Page.Controls

In case you want to count the controls in the page for some reason you have
to iterate recursively , something like this:

int ControlCount( Control parentControl )
{
int current=0;
foreach( Control c in parentControl )
if c.Controls>0
current += ControlCount( c );

return current + parentControl.Controls.Count;



What is what you are trying to do anyway?
 
Back
Top