Common Extend tasks
Common_ExtendTasks_CSharp\App_Code\Utility.cs
// Copyright 2010 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 

public static class Utility
{
    internal static System.Web.UI.Control FindControl(string controlID, System.Web.UI.Page page)
    {
        // Make sure the passed-in page and control are not null
        if (page == null || controlID == null) return null;

        // Try getting a reference to the control being sought via the page's FindControl function
        System.Web.UI.Control webControl = page.FindControl(controlID);

        // Check whether the control was found
        if (webControl == null)
        {
            // Call method to traverse the Page's controls and find the unique id of the control
            // having the passed in control ID
            string uniqueControlID = GetControlUniqueID(controlID, page.Controls);
            if (uniqueControlID != null)
                webControl = page.FindControl(uniqueControlID);
            else
                webControl = page.FindControl(controlID);
        }
        return webControl;
    }

    internal static string GetControlUniqueID(string controlID, System.Web.UI.ControlCollection controls)
    {
        // Declare a Control object to store references to controls in the passed-in ControlCollection
        System.Web.UI.Control control;
        // Declare a string variable to store references to the UniqueIDs of controls in the passed-in
        // collection
        string uniqueID = null;

        // Iterate through the controls in the passed-in collection
        for (int i = 0; i < controls.Count; ++i)
        {
            // Get a reference to the current control
            control = controls[i];

            // Check whether the current control's ID matches the passed in control ID
            if (control.ID == controlID)
            {
                // The control's ID matches, so get a reference to its UniqueID and exit the loop
                uniqueID = control.UniqueID;
                break;
            }

            // Check whether the current control contains any child controls
            if (control.Controls.Count > 0)
            {
                // Recursively call GetControlUniqueID with the passed-in control ID and the current
                // control's collection of child controls
                uniqueID = GetControlUniqueID(controlID, control.Controls);
                // Check whether the ID was found.  If so, exit the loop
                if (uniqueID != null)
                    break;
            }
        }
        return uniqueID;
    }  
}