Click here to Skip to main content
Licence CPOL
First Posted 3 Aug 2010
Views 7,228
Bookmarked 12 times

Building a Better FindControl

By | 3 Aug 2010 | Technical Blog
Some extension methods to try and get around the (many) issues that I’ve had with FindControl
A Technical Blog article. View original blog here.[^]

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)

About the Author

Martin Jarvis

Software Developer (Senior)
Freestyle Interactive Ltd
United Kingdom United Kingdom

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSweet PinmemberArchAngel1238:22 10 Aug '10  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 4 Aug 2010
Article Copyright 2010 by Martin Jarvis
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid