Per OP request...
This code search in an ASP.NET control tree for one with the specific ID.
Such control tree is built for the classic recursive search as every node looks exactly the same, but here I show a not-recursive approach.
I also used the very same idea in a pixel-level search inside an image where I also measured performance - the non-recursive performed a hundred times better...
public static Control FindControlById ( Control oParent, string szID )
{
Control oControl = null;
Stack oStack = new Stack( );
oStack.Push( oParent );
while ( oStack.Count != 0 )
{
Control oCheckControl = ( Control )oStack.Pop( );
if ( oCheckControl.ID == szID )
{
oControl = oCheckControl;
break;
}
else if ( oCheckControl.HasControls( ) )
{
foreach ( Control oChildControl in oCheckControl.Controls )
{
oStack.Push( oChildControl );
}
}
}
return ( oControl );
}