I've been asking for a recursive FindControl() method as a method off of System.Web.UI.Control for years but so far no luck. You find yourself needing these frequently when you work with composite controls, like most of the Login family of controls introduced with ASP.NET 2.0. In particular, LoginView, CreateUserWizard, and Login frequently require a technique like this to access their contents.
I posted a simple version a while back; Michael Palermo updated it, and Aaron Robson posted a nice generic version on IntrepidNoodle.com. However, his site is often incredibly slow or non-responsive (perhaps he should switch to ORCSWeb), so while I'm happy to give him full credit, I'm going to mirror the content here so that I have access to it when I need it.
Of course, this can also be done as an Extension Method, which I'll show at the bottom of this post.
With this in place, you can easily access it from your Page by using a Base Page Class and adding these methods to it:
The next logical step is to just make this an extension method. Brendan Enrick has already done this work for us, so I'll let you read his post to see how it's done.
Update 23 Sep 2008 in response to Howdy Doody's comment
Apologies - I left out how (and perhaps more importantly, why) you would use this. We'll start with the why, which is that you have some some code that nests controls within controls. For example, the Login control has a Password textbox inside of it (whether you template it or not). If you want to get to this control to manipulate it, you can't just drop into Page_Load and say Password.Text = "foo". Password is not defined at Page scope - it's only defined within the control tree of the Login control. This can be a problem with any arbitrary control of your own if you use LoginView or MultiView controls as well. For instance, this LoginView:
<asp:LoginView runat="server" id="LoginView1">
<asp:Label runat="server" id="AdminMessageLabel" />
If you want to set the contents of the AdminMessageLabel in Page_Load, you would not easily be able to do so. However, you can get a reference to it like so, using a recursive FindControl<T> method, which in this case we'll assume is defined either in the page itself or in a common Base Page Class (recommended).
Label AdminMessageLabel = this.FindControl<Label>(LoginView1, "AdminMessageLabel");
if(AdminMessageLabel != null)
AdminMessageLabel.Text = "You are an admin.";
Note that when the user is not an administrator, you won't have this control, so you need to check for null. You also could pass in the Page as the starting position for the search for the control, but that would be much less efficient, and since we know it's in the LoginView1 control's collection of controls, we pass that for efficiency's sake.
Hope that helps - note that this last part was written via a web browser with no compiler access, so apologies in advance for any typos.