ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

GetChildren

This is a recursive function to get all child controls under the target control.

Source

public static IEnumerable<Control> GetChildren(this Control control)
{
  var children = control.Controls.Cast<Control>();
  return children.SelectMany(GetChildren).Concat(children);
}

Example

IEnumerable<Control> children = Page.GetChildren();

or

IEnumerable<Control> children = Container.GetChildren();

Author: Ryan Helms

Submitted on: 17 jul 2010

Language: C#

Type: System.Control

Views: 6458