Monday, 10 January 2011

Finding control inside repeater's footer

Recently I needed 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 it only included the actual databound items, not the footer-item.
aspx.cs:
<asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
                Item
        </ItemTemplate>
        <FooterTemplate>
                Footer
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </FooterTemplate>
</asp:Repeater>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

Right way to do that was this way:
RepeaterItem riFooter=Repeater1.Controls[Repeater1.Controls.Count-1] as RepeaterItem;
if (riFooter != null && riFooter.ItemType == ListItemType.Footer) {
    TextBox TextBox1 = riFooter.FindControl("TextBox1") as TextBox;
    if (TextBox1 != null) {
        TextBox1.Text = "Test";
    }
}

No comments:

Post a Comment