Find a control on a page with a master page
Try this Table tblForm = this.Master.FindControl(“MainContent”).FindControl(“formtable”) as Table; Checkout this Control ID Naming in Content Pages for more details
Try this Table tblForm = this.Master.FindControl(“MainContent”).FindControl(“formtable”) as Table; Checkout this Control ID Naming in Content Pages for more details
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
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
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); }
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
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
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; } }
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