I was wondering how one would find the controls in the HeaderTemplate or FooterTemplate of an Asp.Net Repeater control.
I can access them on the ItemDataBound event, but I was wondering how to get them after (for example to retrieve a value of an input in the header/footer).
Note: I posted this question here after finding the answer just so that I remember it (and maybe other people might find this useful).
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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); } public static Control FindControlInFooter(this Repeater repeater, string controlName) { return repeater.Controls[repeater.Controls.Count - 1].Controls[0].FindControl(controlName); } }
Method 2
Better solution
You can check item type in ItemCreated event:
protected void rptSummary_ItemCreated(Object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Footer) { e.Item.FindControl(ctrl); } if (e.Item.ItemType == ListItemType.Header) { e.Item.FindControl(ctrl); } }
Method 3
You can take a reference on the control on the ItemCreated event, and then use it later.
Method 4
Find control into Repeater (Header, Item, Footer)
public static class FindControlInRepeater { public static Control FindControl(this Repeater repeater, string controlName) { for (int i = 0; i < repeater.Controls.Count; i++) if (repeater.Controls[i].Controls[0].FindControl(controlName) != null) return repeater.Controls[i].Controls[0].FindControl(controlName); return null; } }
Method 5
This is in VB.NET, just translate to C# if you need it:
<Extension()> Public Function FindControlInRepeaterHeader(Of T As Control)(obj As Repeater, ControlName As String) As T Dim ctrl As T = TryCast((From item As RepeaterItem In obj.Controls Where item.ItemType = ListItemType.Header).SingleOrDefault.FindControl(ControlName),T) Return ctrl End Function
And use it easy:
Dim txt as string = rptrComentarios.FindControlInRepeaterHeader(Of Label)("lblVerTodosComentarios").Text
Try to make it work with footer, and items controls too =)
Method 6
The best and clean way to do this is within the Item_Created Event :
protected void rptSummary_ItemCreated(Object sender, RepeaterItemEventArgs e) { switch (e.Item.ItemType) { case ListItemType.AlternatingItem: break; case ListItemType.EditItem: break; case ListItemType.Footer: e.Item.FindControl(ctrl); break; case ListItemType.Header: break; case ListItemType.Item: break; case ListItemType.Pager: break; case ListItemType.SelectedItem: break; case ListItemType.Separator: break; default: break; } }
Method 7
private T GetHeaderControl<T>(Repeater rp, string id) where T : Control { T returnValue = null; if (rp != null && !String.IsNullOrWhiteSpace(id)) { returnValue = rp.Controls.Cast<RepeaterItem>().Where(i => i.ItemType == ListItemType.Header).Select(h => h.FindControl(id) as T).Where(c => c != null).FirstOrDefault(); } return returnValue; }
Finds and casts the control. (Based on Piyey’s VB answer)
Method 8
For ItemDataBound
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Header)//header { Control ctrl = e.Item.FindControl("ctrlID"); } else if (e.Item.ItemType == ListItemType.Footer)//footer { Control ctrl = e.Item.FindControl("ctrlID"); } }
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0