LINQ – The Uber FindControl
March 21st, 2008 by kerrysoft and tagged , API, CRM software, ERP solution, WebAlso see: Blogs at work
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: Versioning/Deploying Unmanaged Files
Also see: From C# to Java: Part 3
Also see: Uniqueness Typing Simplified
Also see: Alexbarn Leaves Microsoft…ARGH!
Also see: Single source code base for Silverlight and WPF solutions
Also see: Generating WPF Content with LINQ
Also see: From C# to Java: Part 4
Also see: ASP.NET MVC in CodePlex and Extensible Unit Testing
Also see: I love ClearContext!!
Also see: Versioning/Deploying Unmanaged Files
Also see: Using IronPython for Dynamic Expressions.
Also see: The influence of style upon methodology…
Also see: The Exception Model
Also see: Chicago geek dinner 11/22
Also see: Uniqueness Typing Simplified
ero.
http://weblogs.asp.net/dfindley/archive/2007/06/29/linq-the-uber-findcontrol.aspx
Posted in Technology | No Comments »