Search code examples
asp.netrepeater

Getting value in footer template from code behind-Repeater


I have a repeater like this:

<asp:Repeater runat="server" ID="pde">
<HeaderTemplate></HeaderTemplate>

<ItemTemplate>
<asp:Literal runat="server" ID="literal1"></asp:Literal>
</ItemTemplate>

<FooterTemplate><asp:Literal runat="server" ID="literal2"></asp:Literal></FooterTemplate>
</asp:Repeater>

Now in literal2 I want to get the value from code behind.But I am not able to get the literal2 in code behind.Any idea how to get this?


Solution

  • You should be able to access it by accessing the last RepeaterItem in your repeater which will be your footer. Then do a search, using FindControl, on the RepeaterItem for any control you are looking for.

    Using your example above do:

    Literal controlYouWant = pde.Controls[pde.Controls.Count - 1].FindControl("literal2") as Literal;
    

    You can break down the statements to:

    // This will get you the footer
    RepeaterItem item = pde.Controls[pde.Controls.Count - 1];
    // From here you finds any control you want within the RepeaterItem.
    Literal controlYouWant = item.FindControl("literal2") as Literal;