Click here to Skip to main content
15,884,176 members
Articles / Web Development / ASP.NET

Building a Better FindControl

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
3 Aug 2010CPOL 12.8K   12   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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Freestyle Interactive Ltd
United Kingdom United Kingdom
I'm a lead developer for Freestyle Interactive Ltd where we create many wonderful websites built on Microsofts ASP.Net and Ektron CMS.

I've been developing .Net applications (both Windows and Web) since 2002.

Comments and Discussions

 
GeneralSweet Pin
ArchAngel12310-Aug-10 8:22
ArchAngel12310-Aug-10 8:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.