LINQ - The Uber FindControl

With a simple extension method to ControlCollection to flatten the control tree you can use LINQ to query the control tree:

public static class PageExtensions
{
    public static IEnumerable<Control> All(this ControlCollection controls)
    {
        foreach (Control control in controls)
        {
            foreach (Control grandChild in control.Controls.All())
                yield return grandChild;

            yield return control;
        }
    }
}
Now I can do things like this:
// get the first empty textbox
TextBox firstEmpty = accountDetails.Controls
    .All()
    .OfType<TextBox>()
    .Where(tb => tb.Text.Trim().Length == 0)
    .FirstOrDefault();

// and focus it
if (firstEmpty != null)
    firstEmpty.Focus();

Pretty cool! I can do all sorts of querying of the control tree now. LINQ you are my hero.

Published Friday, June 29, 2007 9:14 AM by findleyd

Comments

# re: LINQ - The Uber FindControl

Tuesday, July 03, 2007 1:45 PM by ChrisKo

I think your Zelda fetish has gotten a bit out of control. :D

You might need to reformat your code or use a smaller font for the post as it's being cut off on the right side. I think people can still understand the idea with what's shown, but it would be nice to see all the code.

# re: LINQ - The Uber FindControl

Tuesday, July 03, 2007 11:23 PM by ChrisKo

Hmmmm here at home with Firefox, it seems to have a nice scrollable DIV, guess it was IE6 at work.

# re: LINQ - The Uber FindControl

Wednesday, September 26, 2007 6:51 AM by Aykut Sevim

How can we activate this extension method? I think there already exist an All() method in Page class and I cannot make use of your method.

# re: LINQ - The Uber FindControl

Saturday, September 29, 2007 8:45 AM by findleyd

If you put the PageExtensions class inside a namespace you'll need to make sure you have a using for that namespace. This adds the All method to the Controls collection and not the Page class so you should be ok.

# LINQ - The Uber FindControl

Sunday, November 25, 2007 8:47 PM by LINQ - The Uber FindControl

Pingback from  LINQ - The Uber FindControl

# Request for a Better FindControl Method in ASP.NET

Wednesday, March 19, 2008 6:10 PM by WebLog of Ken Cox

&lt;rant&gt; I'm spending hours trying to get a reference to a FileUpload control that's inside an InsertTemplate

# LINQ - The Uber FindControl | Enjoyable Blog of Possibilities

Pingback from  LINQ - The Uber FindControl | Enjoyable Blog of Possibilities

# Daily Find #44 | TechToolBlog

Tuesday, March 25, 2008 10:04 AM by Daily Find #44 | TechToolBlog

Pingback from  Daily Find #44 | TechToolBlog

# findcontrol

Monday, June 30, 2008 3:35 PM by findcontrol

Pingback from  findcontrol

# Recursive Find Control using LINQ

Friday, July 25, 2008 8:17 PM by Brian Chavez's Weblog

Recursive Find Control using LINQ

# re: LINQ - The Uber FindControl

Wednesday, April 29, 2009 5:02 PM by rouftop

Really handy.  Will perf come to bite me in the butt though?

# re: LINQ - The Uber FindControl

Monday, May 25, 2009 5:56 AM by John Chavez

Still cannot get it to work getting this error:

CS1501: No overload for method 'All' takes '0' arguments

Using this code:

Page.Controls.All().OfType<WebControl>().ToList().ForEach(c => c.Enabled = (CardID != 0));

# re: LINQ - The Uber FindControl

Monday, May 25, 2009 11:00 AM by findleyd

Just make sure that whatever namespace PageExtensions is in is in scope. You may need to add a using to your code behind.

# re: LINQ - The Uber FindControl

Tuesday, June 09, 2009 11:06 AM by Marco

Man, I've just thrown away my code... poor.

Leave a Comment

(required) 
(required) 
(optional)
(required)