Find control recursive

19/08/2013 13:49

private Control FindControlRecursive(Control parentControl, string controlId)

{

    Control foundControl = null;

    if (parentControl.Controls != null && parentControl.Controls.Count > 0)

    {

        foundControl = parentControl.FindControl(controlId);

        if (foundControl == null)

        {

            foreach (Control control in parentControl.Controls)

            {

                foundControl = FindControlRecursive(control, controlId);

                if (foundControl != null)

                    return foundControl;

            }

        }

        else

        {

            return foundControl;

        }

    }

    return foundControl;

}