Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

The default implementation of the FindControl method which is part of the Control class is not recursive. This means that if you are trying to find a TextBox control "txtUserName" which is nested inside a FormView control you cannot use the following code:


The reason is that the txtUserName is not contained inside the Page control but actually is a part of the FormView control. So, the only way to access the txtUserName is to use the FindControl method of the FormView control.


Sometimes we just need to get the control without knowing about the parent of the control. In this case we will create our own FindControl method.

public static class ExtensionMethods

        public static T BetterFindControl<T>(this Control root, string id) where T : Control
            if (root != null)
                if (root.ID == id) return root as T;

                var foundControl = (T) root.FindControl(id);
                if (foundControl != null) return foundControl;

                foreach (Control childControl in root.Controls)
                    foundControl = (T) BetterFindControl<T>(childControl, id);
                    if (foundControl != null) return foundControl as T;


            return null;

I have set up the BetterFindControl method as an extension method for the Control class. Now, I can access the "txtUserName" TextBox directly from the Page class using the following code:

var tb = Page.BetterFindControl<TextBox>("txtUserName");

Althought this works just fine but it has side effects. Consider that you page contains lots of Grids and rich controls. And the control that you are trying to find is located at the end of the page. Then using the Page.BetterFindControl('id") would not be a good idea since it will first check all the controls in its path which will kill performance.

So, it is always a good idea to know who is the parent of the searched control. A better use of BetterFindControl<T> method will be when searching inside a Wizard control or any other template based control.

Posted on Sunday, April 20, 2008 2:38 PM | Back to top

Comments on this post: Creating Better FindControl Method

# re: Creating Better FindControl Method
Requesting Gravatar...
Not so Better FindControl Metod

I don’t get it. The reason for using NamingContainer is that you don’t need to concern about how you name your controls; this means that you can have a bunch of user controls that all have a Label named: “label1” – using your Page.FindControl("label1"); will return what? The first label1 it finds… not so better ;-)

Left by Henrik Stenbaek on Apr 21, 2008 1:44 AM

# re: Creating Better FindControl Method
Requesting Gravatar...
Hi Henrik,

That is an interesting comment! I think you are missing the big picture. Let's consider a small example where you have a CreateUserWizard control which contains a textbox named "txtPetsName". How would you access that textbox? You cannot do txtPetsName.Text from the code behind! You cannot do CreateUserWizard1.FindControl("txtPetsName)

What you need is a recursive loop that will look in all the controls inside the CreateUserWizard control. In that case you can use the following to find the control:


You can even use Page.BetterFindControl<TextBox>(id) but I don't think that is a good way since Page might contain tons of other controls and this will kill the performance.

Now, your other question that what if you have two controls with the same name. Well, it will depend on the container which you are searching for. If you are using Page object as a container then the last one with the ID wins (thats why it is not a good idea to use page as a container for findcontrol method). However if you use the CreateUserWizard as a container then it will return the control inside the wizard control.

Left by Mohammad Azam on Apr 21, 2008 4:07 AM

Your comment:
 (will show your gravatar)

Copyright © Mohammad Azam | Powered by: