120

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

mbillard
  • 36,360
  • 18
  • 71
  • 97

8 Answers8

175

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);
    }
}
CDspace
  • 2,551
  • 17
  • 31
  • 35
mbillard
  • 36,360
  • 18
  • 71
  • 97
54

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);
    }
}
David Rogers
  • 2,284
  • 2
  • 30
  • 68
Behtash Moradi
  • 541
  • 4
  • 2
5

You can take a reference on the control on the ItemCreated event, and then use it later.

pascal
  • 61
  • 1
  • 1
  • 13
    You simply asked how you would access it later, the answer pascal gave of setting a reference at that point to use afterwards is perfectly valid. – Kasaku Sep 21 '11 at 16:40
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;
    }
}
Chaki_Black
  • 812
  • 3
  • 11
  • 25
2

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;
            }
    }
Sebastien H.
  • 5,543
  • 2
  • 24
  • 32
2

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

Piyey
  • 473
  • 4
  • 15
0
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)

glboothby
  • 98
  • 8
0

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");
    }
}