Default text for empty Repeater control

AGuyCalledGerald picture AGuyCalledGerald · Mar 11, 2011 · Viewed 54.7k times · Source

Using VS 2008, I have a Repeater control:

<asp:Repeater runat="server" ID="storesRep" DataSourceID="storeSqlDataSource" 
    OnItemDataBound="StoresRep_ItemDataBound">
    <ItemTemplate>
        <table style="padding:0px">
        <tr>
            <td style="width:200px"><asp:Label ID="infoLbl" runat="server">
              Choose stores for upload:</asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;
            </td>
            <td style="width:110px">
              <asp:Label ID="storeLbl" runat="server" Text='<%# Bind("Name") %>'>
              </asp:Label>&nbsp;&nbsp;
            </td>
            <td><asp:CheckBox runat="server" ID="storeCheck" /></td>
        </tr>
        </table>
    </ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="storeSqlDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:someConnectionString %>"
    SelectCommand="SELECT [StoreId], [Name] FROM [Store] Order By [Name]">
</asp:SqlDataSource>

Now I would like to display a default text like "No stores found" if data source returns no items from database. Until now I have mostly used GridView where I didn't have problems because of the EmptyDataText attribute.

Answer

AGuyCalledGerald picture AGuyCalledGerald · Mar 28, 2012

Joaos answer can even be simplified. In the footer, do not set the visible-property of your default item to false, but use the following expression:

<FooterTemplate>
    <asp:Label ID="defaultItem" runat="server" 
        Visible='<%# YourRepeater.Items.Count == 0 %>' Text="No items found" />
</FooterTemplate>

This way, you can save the code behind.