I need to loop through all the controls in my asp.net webpage and do something to the control. In one instance I\'m making a giant string out of the page and emailing it to
Your original method will not work if you start from the root element of your document: something like page.Controls as you will only loop through the first level of controls, but remember a control can be composite. So you need recursion to pull that off.
public void FindTheControls(List foundSofar, Control parent)
{
foreach(var c in parent.Controls)
{
if(c is IControl) //Or whatever that is you checking for
{
foundSofar.Add(c);
if(c.Controls.Count > 0)
{
this.FindTheControls(foundSofar, c);
}
}
}
}