A taste of development

March 24, 2008

LINQ – The Uber FindControl

Filed under: Technology —Tagged , , , , — simma1990 @ 12:00 pm

Also see: Trust Microsoft with Claimspace (my response pending)

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


.csharpcode,.csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode.rem { color: #008000; }
.csharpcode.kwrd { color: #0000ff; }
.csharpcode.str { color: #006080; }
.csharpcode.op { color: #0000c0; }
.csharpcode.preproc { color: #cc6633; }
.csharpcode.asp { background-color: #ffff00; }
.csharpcode.html { color: #800000; }
.csharpcode.attr { color: #ff0000; }
.csharpcode.alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode.lnum { color: #606060; }

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

Also see: Mix08 Session Overview: Building Great AJAX Applications from Scratch Using ASP.NET 3.5 and Visual Studio 2008

Also see: Web Services with Spring 2.5 and Apache CXF

Also see: From C# to Java: Part 4

Also see: Be my Support Group

Also see: LoadFrom’s Second Bind

Also see: Brad Abrams’ pixel8 Interview Podcast posted

Also see: Silverlight 2 Beta 1 Cross Domain Bug

Also see: Brad Abrams’ pixel8 Interview Podcast posted

Also see: Never keep your emotions bottled up

Also see: LearnExpression.com is live.

Also see: Java Frameworks State of the (dis)Union.

Also see: LoadFrom’s Second Bind

Also see: Win friends and influence your team

Also see: Memory Model

Also see: Exception Handling in Running a Business

Also see: Silverlight 2 Beta 1 Cross Domain Bug

Also see: Big in Japan

Also see: New Assembly, Old .NET (and Vice-Versa)

Also see: When Will Foreign Ownership of US Sports Teams Start ?

Also see: Mix08 Session Overview: Building Great AJAX Applications from Scratch Using ASP.NET 3.5 and Visual Studio 2008

Also see: LINQ – The Uber FindControl

Also see: From C# to Java: Part 5

Also see: Be my Support Group

Also see: New Assembly, Old .NET (and Vice-Versa)

Also see: TransparentProxy

Also see: Hello world!

Also see: REST2SQL in a Jiffy, with Tagspace for Spice

Also see: Yes, it does mean everything

Also see: Doing the Deal and Dishing the Dirt

Also see: Music and Movies – Give Away the Soundtrack

Also see: Natural Sorting in C#

ero.


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

Comments are closed.

©2012 A taste of development
Provided by WPMU DEV -The WordPress Experts
Hosted by Edublogs.org