Find all child controls of specific type using Enumerable.OfType() or LINQ

I use an extension method to flatten control hierarchy and then apply filters, so that’s using own recursive method. The method looks like this public static IEnumerable<Control> FlattenChildren(this Control control) { var children = control.Controls.Cast<Control>(); return children.SelectMany(c => FlattenChildren(c)).Concat(children); }

ASP.NET Is there a better way to find controls that are within other controls?

Generally I implement a “FindInPage” or recursive FindControl function when you have lots of control finding to do, where you would just pass it a control and it would recursively descend the control tree. If it’s just a one-off thing, consider exposing the control you need in your API so you can access it directly. … Read more

C#, FindControl [duplicate]

Courtesy of Mr. Atwood himself, here’s a recursive version of the method. I would also recommend testing for null on the control and I included how you can change the code to do that as well. protected void Button1_Click(object sender, EventArgs e) { if (TextBox1.Text != “”) { Label Label1 = FindControlRecursive(Page, “Label1”) as Label; … Read more

How to find controls in a repeater header or footer

As noted in the comments, this only works AFTER you’ve DataBound your repeater. To find a control in the header: lblControl = repeater1.Controls[0].Controls[0].FindControl(“lblControl”); To find a control in the footer: lblControl = repeater1.Controls[repeater1.Controls.Count – 1].Controls[0].FindControl(“lblControl”); With extension methods public static class RepeaterExtensionMethods { public static Control FindControlInHeader(this Repeater repeater, string controlName) { return repeater.Controls[0].Controls[0].FindControl(controlName); } … Read more

Finding control within WPF itemscontrol

Using the ItemContainerGenerator you can obtain the generated container for an item and traverse the visual tree downwards to find your TextBox. In the case of an ItemsControl it will be a ContentPresenter, but a ListBox will return a ListBoxItem, ListView a ListViewItem, etc. ContentPresenter cp = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as ContentPresenter; TextBox tb = FindVisualChild<TextBox>(cp); if … Read more

How to find Control in TemplateField of GridView?

Try this: foreach(GridViewRow row in GridView1.Rows) { if(row.RowType == DataControlRowType.DataRow) { HyperLink myHyperLink = row.FindControl(“myHyperLinkID”) as HyperLink; } } If you are handling RowDataBound event, it’s like this: protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if(e.Row.RowType == DataControlRowType.DataRow) { HyperLink myHyperLink = e.Row.FindControl(“myHyperLinkID”) as HyperLink; } }

Better way to find control in ASP.NET

All the highlighted solutions are using recursion (which is performance costly). Here is cleaner way without recursion: public T GetControlByType<T>(Control root, Func<T, bool> predicate = null) where T : Control { if (root == null) { throw new ArgumentNullException(“root”); } var stack = new Stack<Control>(new Control[] { root }); while (stack.Count > 0) { var … Read more