views:

3956

answers:

2

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

+10  A: 

I already found the answer:

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);
    }
}
GoodEnough
Thanks!I've seen many crappy solutions for this, but this is the best so far!
sshow
Just a small picky note - You need to capitalize the "c" in Controls[0] in your footer example.
Mike C.
Yuck. Is this really the only way?
Andrew Hancox
I haven't found a built-in way.You could build an extension method if you really want.
GoodEnough
Love those extensions methods!
cerhart
A: 

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

pascal