Lookup a control at an arbitrary level of the Page hierarchy

In ASP.NET 2.0 the page hierarchy often happens to be deeper than it was with the older version, mostly when using MasterPages. Consequently, when you want to obtain a reference to a control of the page given its ID and you don't know at which level of the hierarchy it is placed, using the FindControl() method of the Page object isn't the right way of proceding since it just performs a search among the direct children of the page itself.

In such cases you may use a recursive search, which walks all the page hierarchy. Note that you should use this workaround only when you strictly need it, because it could introduce a consistent overhead in the processing of the page due to the fact that everything becomes a control when on the server, and you'll find yourself looping through a considerable number of controls. This is a recursive implementation of the method:
public static Control FindControl(string controlId, ControlCollection controls)
{
      foreach (Control control in controls)
      {
            if(control.ID == controlId)
                  return control;

            if(control.HasControls())
            {
                  Control nestedControl = FindControl(controlId, control.Controls);

                  if(nestedControl != null)
                        return nestedControl;
            }
      }
      return null;
}
Then you can simply use it this way:
Control foundControl = FindControl(controlToFindID, Page.Controls);
Published 03 July 2006 09:45 PM by simoneb
Filed under: ,

Comments

# John Adams said on 03 November, 2006 02:53 PM
This blog entry of your has captured my attention because it looks like it could solve my problem; see: http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_22045489.html I implemented a VB version of this function and it returns nothing (cannot find the control). I have a master page, a content page that uses 2 user controls. One of the user controls contains an ASP.NET 2.0 LoginView and Login control. The Login Control was converted into a template (textboxes for username and password, an ASP:Button control for Login. It is this last button I am trying to locate so that I can make it the default button: Dim foundControl As Control = FindMyControl("LoginButton", Page.Controls) Page.Form.DefaultButton = foundControl.UniqueID My VB adaptation of your C# code I simply named FindMyControl but it returns nothing. This code is declared in my usercontrol.ascx.vb and is invoked in the Page_Load of that file. This entire pursuit is attempting to "automatically" click the Login button when the user finishes typing the password and simply hits ENTER key. If you can help, it would be great. I am at johna@cbmiweb.com
# simoneb said on 13 November, 2006 09:38 PM

Hi John, sorry for the late reply. I gave a wuick look at your post but I don't see why you cannot find the control. Using this recursive method you should be able to find any control in the page.

# Johan said on 11 June, 2007 02:31 AM
BRILLIANT!!!
The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.

This site

Search

Go

This Blog

News

     

    CS2
    Checkout CS2, my academic project about indexing and searching personal source code with Lucene.Net.

    Windows Developer Power Tools

Syndication

Sponsors

  • MaximumASP
  • Packet Sniffer
    Home Loan
  • conference call review