Software Geek

March 22, 2008

LINQ - The Uber FindControl

Filed under: Software

Also see: Using IronPython for Dynamic Expressions.

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 h

Also see: LINQ - The Uber FindControl

Also see: Uniqueness Typing Simplified

Also see: Parallels adds “Express Windows Installation”

Also see: The obligatory Halo 2 partial review and thumbs up.

Also see: The influence of style upon methodology…

Also see: Uniqueness Typing Simplified

Also see: Stand alone FXCop download

Also see: ASP.NET MVC in CodePlex and Extensible Unit Testing

Also see: From C# to Java: Part 4

Also see: LINQ - The Uber FindControl

Also see: Parallels adds “Express Windows Installation”

Also see: LINQ - The Uber FindControl

Also see: Memory Model

Also see: Single source code base for Silverlight and WPF solutions

Also see: Solving big business problems in our little toolbox application. A use case for Project Distributor.

Also see: C# 3.0 Lambdas and Type Inference

ero.
http://weblogs.asp.net/dfindley/archive/2007/06/29/linq-the-uber-findcontrol.aspx

Comments »

The URI to TrackBack this entry is: http://annil12.blogsome.com/2008/03/22/linq-the-uber-findcontrol-2/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Get free blog up and running in minutes with Blogsome
Theme designed by Jay of onefinejay.com