Custom FindControl Implementation (C#)

Anyone who has ever used the native FindControl method built into the System.Web.UI.Page or System.Web.UI.Control classes knows that it pretty much stinks. From my experience using it, the only controls that it can find by name are those that are direct children of the container being searched. This isn’t helpful for most real world ASPX pages which may contain dozens of nested controls and/or third party tools.

So, here’s my own custom version of what FindControl should have been from the beginning.

First, override the FindControl method in your derived control and/or webform base class.

        public override Control FindControl(string id)
            Control bc = null;
                bc = base.FindControl(id);
                bc = null;
            return (bc != null) ? bc : MyUtility.FindControl(id, this.Controls);

Now write the static utility methods to implement the recursive search if the native FindControl didn’t get what you wanted.

    public class MyUtility
        public static Control FindControl(string id, ControlCollection col)
            foreach (Control c in col)
                Control child = FindControlRecursive(c, id);
                if (child != null)
                    return child;
            return null;

        private static Control FindControlRecursive(Control root, string id)
            if (root.ID != null && root.ID == id)
                return root;

            foreach (Control c in root.Controls)
                Control rc = FindControlRecursive(c, id);
                if (rc != null)
                    return rc;
            return null;

Voila! Now you have the ability to find controls by name that are nested in the hairiest control structure you can come up with.

Check out Part II of this topic to see how to extend this implementation to find multiple controls by Type.



Filed under ASP.NET, C#, Programming

11 responses to “Custom FindControl Implementation (C#)

  1. Pingback: Leading the Next Inquisition » Blog Archive » Custom FindControl Implementation (C#) — Part II

  2. Krishan

    Good day,

    I’m using .net 2 and master page concept. I couldnt grab the object from Page.PreviousPage.FindControl() method. since its wraped in a ContentControl. The only way was to use a customer FindControl. And your code save me some time.

    Cheers Mate

  3. This worked like a damn charm. Thanks.

  4. ReadyForTheNextInquisition

    Excellent! Excellent! Excellent!

  5. Nick

    What happens if your custom control contains a datagrid which has recurring controls. Wouldn’t this always return the first occurance? Might be an interesting exercise to try.

    If I get some time, I will try it and post my results back here.

  6. Thanks ! This custom findcontrol is a great idea !

  7. Sharath

    This is really use full class. Thanks for your help

  8. Speaker-to-Animals

    Man, that is absolutely beautiful! Thanks for solving a major headache!

  9. Nice post! My own FindControlRecursive helped me out many times. Especially, when I deal with MasterPages. For example, one of such usage is shown in my post here –
    Thank you!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s