Hiding table rows in a repeater

04 Dec
December 4, 2009

Here’s a simple tip to clean up the way you display data in a repeater, by hiding rows that do not contain any data.  As an example, you may have a field in your database such as Middle Name which you want to hide if the value is empty.

In this case you may have a repeater that shows information this information in a table format like so:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
    <ItemTemplate>
        <table style="width: 100%;">
            <tr>
                <td>
                    First name
                </td>
                <td>
                     %< Eval("FirstName")%>
                </td>
            </tr>
            <tr>
                <td>
                    Middle name
                </td>
                <td>
                     %< Eval("MiddleName")%>
                </td>
            </tr>

            ... etc etc 

        </table>
    </ItemTemplate>
</asp:Repeater>
In order to only show the middle name row if there is data in the field, then change the <TR> properties like so…
<tr id="TR1" runat="server" visible='<%# ContainsData(Eval("MiddleName")) %>'>
    <td>
        Middle name
    </td>
    <td>
         Eval("MiddleName")%>
    </td>
</tr>
… and then create the following function:
Public Function ContainsData(ByVal data As Object) As Boolean
    Return Not (IsDBNull(data) Or data.ToString = "")
End Function

Hope this helps.

Tags:
© Copyright - Evonet