Change Telerik RadGrid CommandItemTemplate controls on page load with a recursive FindControl function

If you want to change the controls in the CommandItemTemplate of a Telerik RadGrid, you will have to use a recursive FindControl function, because ASP .NET 2.0 does not ship with a FindControl function that can search for controls in child controls.

     public static class ControlFinder
    {
        /// <summary>
        /// Recursively finds a control in the controls collection.
        /// </summary>
        /// <param name="control"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Control FindControlRecursive(Control control, string id)
        {
            // Return null if parameter control is null
            if (control == null) return null;

            // Try to find the control at the current level
            Control ctrl = control.FindControl(id);
            if (ctrl == null)
            {
                // Loop through child controls
                foreach (Control child in control.Controls)
                {
                    // Try to find the control at the next level
                    ctrl = FindControlRecursive(child, id);

                    // Stop search when control is found
                    if (ctrl != null) break;
                }
            }
            return ctrl;        }
    }

In the page load:

LinkButton showReports = ControlFinder.FindControlRecursive(this.RadGrid.MasterTableView, "showReports") as LinkButton;
showReports.Attributes["href"] = @"\MyShare";