示例#1
0
        /*  Misc
         *  -----------------------------------------------------------------------------------------------*/

        public static Control FindControlByClientID(Control seed, string clientID, bool traverse, Control branch)
        {
            if (seed == null || string.IsNullOrEmpty(clientID))
            {
                return(null);
            }

            Control parent = (seed is INamingContainer) ? seed : seed.NamingContainer;

            if (clientID.Equals(parent.ClientID ?? ""))
            {
                return(parent);
            }

            Control found        = null;
            string  exclude      = (branch != null) ? branch.ClientID ?? "" : "";
            string  tempID       = "";
            string  tempClientID = "";

            List <Control> waiting = new List <Control>();

            foreach (Control c in parent.Controls)
            {
                tempID       = c.ID ?? "";
                tempClientID = c.ClientID ?? "";

                if (clientID.Equals(tempID) || clientID.Equals(tempClientID))
                {
                    found = c;
                }
                else if (ControlUtils.HasControls(c) && !exclude.Equals(tempClientID))
                {
                    found = ControlUtils.FindChildControlByClientID(c, clientID);
                }

                if (found != null)
                {
                    break;
                }
            }

            if (traverse && found == null)
            {
                found = ControlUtils.FindControlByClientID(parent.NamingContainer, clientID, true, parent);
            }

            return(found);
        }