Live Chat Programs

March 23, 2008

LINQ - The Uber FindControl

Filed under: Live Chat software

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();


Softwre Development for small and middle size companies. World-class software applications.

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

Also see: Infrequent blogging

Also see: Alexbarn Leaves Microsoft…ARGH!

Also see: Eriskay: a Programming Language Based on Game Semantics

Also see: Java Concurrency, another series on its issues

Also see: Important changes to the BASE element for IE 7

Also see: Snippet Compiler update

Also see: From C# to Java: Part 5

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

Comments »

The URI to TrackBack this entry is: http://thelivechatsoftware.blogsome.com/2008/03/23/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