Asked on Forums. Point is that say you have a Repeater like this:
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<asp:Label ID="lblHeaderDisplay" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<br />
Test<br />
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblFooterDisplay" runat="server" />
</FooterTemplate>
</asp:Repeater>
and you want to access Labels on header and footer in code. Of course, it's trivial that you can access them in ItemCreated - which is fine, only drawback being that viewstate isn't yet tracked at that point - or ItemDataBound - which probably works for almost all scenarios -
You could also use approach as follows, in case you need to access them elsewhere, if for some reason Itemxxx events don't work for you. You just need to have the Repeater databound before as otherwise header or footer templates aren't yet instantiated
...
Repeater1.DataBind();
Label lblHeader=Repeater1.Controls[0].FindControl("lblHeaderDisplay") as Label;
if (lblHeader != null)
lblHeader.Text = "Hello world from header";
Label lblFooter = Repeater1.Controls[Repeater1.Controls.Count -1].FindControl("lblFooterDisplay") as Label;
if (lblFooter != null)
lblFooter.Text = "Hello world from footer";
Share this post: email it! | bookmark it! | digg it! | reddit! | kick it! | live it!