Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Building a Better FindControl

, 3 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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 

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.

 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….

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

 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)

Share

About the Author

Martin Jarvis
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.
Follow on   Twitter

Comments and Discussions

 
GeneralSweet PinmemberArchAngel12310-Aug-10 8:22 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 4 Aug 2010
Article Copyright 2010 by Martin Jarvis
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid