The FindControl method is one of the most commonly used methods in ASP.NET development. The purpose of FindControl method is to find the ASP.NET control on the Page control. It is also used to find the nested control within a particular ASP.NET control. The problem with the default implementation of the FindControl is that it fails to find the control when nested deep in the hierarchy. This results in problem when looking for controls inside the more complicated controls in ASP.NET which includes Wizard, Login controls etc.

FindControl Limitations:

We will use a simple CreateUserWizard control and add a TextBox control named "TextBox1" inside it. The TextBox control is inserted deep into the control tree as shown in the following code:



The page also uses the master page and everything is placed inside the ContentPlaceHolder control. Now try accessing the "TextBox1" control by the default FindControl method as shown in the code below:



This will result in NullException since the control "TextBox1" is not found inside the CreateUserWizard control. This proves the limitation of the FindControl method. Now, let's see how we can implement a custom FindControl method to solve this problem.

Creating BetterFindControl Method:

The BetterFindControl method is implemented as an extension method and placed in the ExtensionMethods namespace. We have used generics to support strong typing. The implementation of BetterFindControl is listed below:



The BetterFindControl methods use recursion to check the child controls. If the child control is found then it is sent back again to find further child controls. This loop is continued until the control with the appropriate ID is found.

We can use the BetterFindControl method as shown below:



This solves the problem of looking for a control is a complicated hierarchy of controls. If you are using .NET 3.0 then you can make use of lambda expressions. The MuchBetterFindControl implementation uses lambda expressions.

Creating MuchBetterFindControl:

We will be using Func<T> to represent a method then can be passed as a parameter without explicitly declaring a custom delegate. The implementation is shown below:



The interesting thing to note about the MuchBetterFindControl is the second parameter which is a delegate. The Func<T,bool> simply means that it is a method that takes in a type T and returns a Boolean value. It is equal to the following piece of code.



The usage of the MuchBetterFindControl is shown below:



You can also create overload for MuchBetterFindControl to support the string ID as shown below:



The MuchBetterFindControl can be passed multiple arguments to filter the search. The code below shows the MuchBetterFindControl searching on CssClass and the ID of the control.

    

Conclusion:

In this article we learned to create a better FindControl method which recursively search in the control tree. In normal scenarios you should always use FindControl since it will be faster because of non recursive looping but in rare circumstances you can incline towards the BetterFindControl and MuchBetterFindControl to locate a particular control. 

[Download Sample]