Friday, August 12, 2011

How to find control recursively from controls collection?

To find control from controls collection using recursive method you can use following static method. I have used this method in my project.

 public static Control FindControlRecursive(Control parent, string controlId)
        {
            if (parent.ID == controlId)
                return parent;

            foreach (Control obj in parent.Controls)
            {
                if (obj.HasControls())
                {
                    Control foundControl  = FindControlRecursiveDown(obj, controlId);
                    if (foundControl != null)
                        return foundControl;
                }
            }

            return null;
        }


Thanks

No comments:

Post a Comment