Search code examples
asp.netrepeaterfooter

Why is the footer-item not included in Repeater.Items?


I need to get a value from a textbox inside a FooterTemplate in the OnClick event of a button. My first thought was to loop through the items-property on my repeater, but as you can see in this sample, it only includes the actual databound items, not the footer-item.

ASPX:

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        Item<br />
    </ItemTemplate>
    <FooterTemplate>
        Footer<br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </FooterTemplate>
</asp:Repeater>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

Code-behind.cs:

protected void Page_Load(object sender, EventArgs e)
{
    ListItemCollection items = new ListItemCollection();
    items.Add("value1");
    items.Add("value2");
    Repeater1.DataSource = items;
    Repeater1.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine(Repeater1.Items.Count);
}

This code will only output "2" as the count, so how do I get to reference my textbox inside the footertemplate?


Solution

  • From the MSDN documentation, the Items is simply a set of RepeaterItems based off the DataSource that you are binding to and does not include items in the Header or FooterTemplates.

    If you want to reference the textbox, you can get a reference on ItemDataBound event from the repeater where you can test for the footer.

    E.g.

    private void Repeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e) 
    {
    
      if (e.Item.ItemType == ListItemType.Footer) 
      {
        TextBox textBox = e.Item.FindControl("TextBox1") as TextBox;
      }
    }