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

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 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

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

If you’re looking for a specific type of control you could use a recursive loop like this one – http://weblogs.asp.net/eporter/archive/2007/02/24/asp-net-findcontrol-recursive-with-generics.aspx Here’s an example I made that returns all controls of the given type /// <summary> /// Finds all controls of type T stores them in FoundControls /// </summary> /// <typeparam name=”T”></typeparam> private class ControlFinder<T> where … Read more