Tuesday, November 25, 2008

Count Controls in a Page in C#

Here is a simple method I created to count all the controls in a C#.NET page recursively.

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

No comments: