How to find controls in a repeater header or footer

asp.netControlsRepeaterFindcontrol

asp.net Problem Overview


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

asp.net Solutions


Solution 1 - asp.net

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

Solution 2 - asp.net

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

Solution 3 - asp.net

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

Solution 4 - asp.net

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

Solution 5 - asp.net

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

Solution 6 - asp.net

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

Solution 7 - asp.net

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)

Solution 8 - asp.net

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmbillardView Question on Stackoverflow
Solution 1 - asp.netmbillardView Answer on Stackoverflow
Solution 2 - asp.netBehtash MoradiView Answer on Stackoverflow
Solution 3 - asp.netpascalView Answer on Stackoverflow
Solution 4 - asp.netChaki_BlackView Answer on Stackoverflow
Solution 5 - asp.netPiyeyView Answer on Stackoverflow
Solution 6 - asp.netSebastien H.View Answer on Stackoverflow
Solution 7 - asp.netglboothbyView Answer on Stackoverflow
Solution 8 - asp.netClaudinei FerreiraView Answer on Stackoverflow