Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Building a Better FindControl

0.00/5 (No votes)
3 Aug 2010 1  
Some extension methods to try and get around the (many) issues that I’ve had with FindControl

There are times in any semi-advanced ASP.NET developer's life when they’re working with databound templated controls (such as the Repeater) and they need to find a control without knowing where it is, or even if it's there.

Normally, you’re reduced to using the standard FindControl method available on any control. But this only finds controls with a given id within the same NamingContainer.

I’ve written some extension methods to try and get around the (many) issues that I’ve had with FindControl and hopefully people will find them useful.

Get All Child Controls of a Given Type 

C#
public static IEnumerable<T> GetAllChildControlsOfType<T>
	(this Control parent) where T : Control
 {
    if (parent == null) throw new ArgumentNullException("parent");
    foreach (Control c in parent.Controls)
    {
        if (typeof(T).IsInstanceOfType(c)) yield return (T)c;
        foreach (T tc in c.GetAllChildControlsOfType<T>())
            yield return tc;
    }
    yield break;
 } 

Get All Child Controls that Implement a Given Interface

For when you don’t care what the control is, as long as it can do a particular thing….try it with IButtonControl.

C#
public static IEnumerable<T> GetAllChildControlsWithInterface<T>(this Control parent)
{
   if (!typeof(T).IsInterface) throw new NotSupportedException
   (string.Format(CultureInfo.InvariantCulture,"Type '{0}'
   is not an interface".ToFormattedString(typeof(T).ToString()));
   if (parent == null) throw new ArgumentNullException("parent");
   foreach (object c in parent.Controls)
   {
       if (typeof(T).IsInstanceOfType(c))
           yield return (T)c;
       Control ctrl = c as Control;
       if (ctrl != null)
           foreach (T tc in ctrl.GetAllChildControlsWithInterface<T>())
               yield return tc;
   }
   yield break;
}

Find All Controls With a Given ID

Find every Textbox with an Id of ‘FirstName’ within a repeater….

C#
public static IEnumerable<T> FindAllControl<T>
	(this Control parent, string id) where T : Control
 {
    if (string.IsNullOrEmpty(id)) throw new ArgumentNullException("id");
    return parent.GetAllChildControlsOfType<T>()
        .Where(c => string.Equals(c.ID, id, StringComparison.OrdinalIgnoreCase));
 } 

Find The First Control with an ID

C#
public static T FindControl<T>(this Control parent, string id) where T : Control
{
   return parent.FindAllControl<T>(id).FirstOrDefault();
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here