Search code examples
c#asp.netwebformsrepeater

Hide (make invisible) repeater footer and/or header


Is there anyway I can hide a footer and/or header in a repeater?

Basically I have two repeaters bound to different datasources one on top of the other:

<asp:Repeater runat="server" ID="rptAdditionalCosts">
        <HeaderTemplate>
            <table id="optionalExtrasPriceBreakdown" cellpadding="0" cellspacing="0" border="0">
                <thead>
                    <tr>
                        <th colspan="4"> Additional Costs </th>
                    </tr>
                    <tr>
                        <th> Description </th>
                        <th> Quantity </th>
                        <th> Price </th>
                        <th class="totalPrice"> Total Price </th>
                    </tr>
                </thead>
                <tbody>
        </HeaderTemplate>
        <ItemTemplate>
                    <tr>
                        <td><%#DataBinder.Eval(Container.DataItem, "Description")%></td>
                        <td><%#DataBinder.Eval(Container.DataItem, "Count")%></td>
                        <td>£<%# DataBinder.Eval(Container.DataItem, "Gross", "{0:n2}")%></td>
                        <td class="totalPrice">£<%#DataBinder.Eval(Container.DataItem, "Total", "{0:n2}")%></td>
                    </tr>
        </ItemTemplate>
        <FooterTemplate>
                </tbody>
            </table>
            <hr class="spacer" />
        </FooterTemplate>
    </asp:Repeater>
    <asp:Repeater runat="server" ID="rptOptionalExtras">
        <HeaderTemplate>
            <table id="optionalExtrasPriceBreakdown" cellpadding="0" cellspacing="0" border="0">
                <thead>
                    <tr>
                        <th colspan="4"> Additional Costs </th>
                    </tr>
                    <tr>
                        <th> Description </th>
                        <th> Quantity </th>
                        <th> Price </th>
                        <th class="totalPrice"> Total Price </th>
                    </tr>
                </thead>
                <tbody>
        </HeaderTemplate>
        <ItemTemplate>
                    <tr>
                        <td><%#DataBinder.Eval(Container.DataItem, "Description")%></td>
                        <td><%#DataBinder.Eval(Container.DataItem, "Number")%></td>
                        <td>£<%# DataBinder.Eval(Container.DataItem, "UnitCost", "{0:n2}")%></td>
                        <td class="totalPrice">£<%#DataBinder.Eval(Container.DataItem, "TotalCost", "{0:n2}")%></td>
                    </tr>
        </ItemTemplate>
        <FooterTemplate>
                </tbody>
            </table>
            <hr class="spacer" />
        </FooterTemplate>
    </asp:Repeater>

If they both contain data I want to hide the footer in the earlier and the header in the later, thus it makes one complete table in the HTML. I have bools that tells me if they contain data.

if (optionalVisible && additionalVisible)
{
     //hide rptAdditionalCosts footer
     //and
     //hide rptPerBookingOptionalExtras header
}

Problem is the repeater doesn't seem to contain any options to hide or display the footer or header templates? Am I missing something glaringly obvious?


Solution

  • In the end I managed to get something working using the ItemDataBound event of the repeater:

    void rptAdditionalCosts_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            if (optionalVisible && additionalVisible)
                e.Item.Visible = false;
        }
    }
    
    void rptOptionalExtras_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Header)
        {
            if (optionalVisible && additionalVisible)
                e.Item.Visible = false;
        }
    }
    

    Thanks to @KarlAnderson for the answer, I never actually checked his solution it because I'd just got this running.