Search code examples
c#vb.netrepeatervisible

Im trying to make a Repeater table visible = false


I have a document management system which creates a report showing people who own which document. There are times where people have 0 documents and in that case I would like the repeater table for that person to not be visible. I have looked around for a while and have not had much luck, maybe its because I am new or maybe its because I havent found my answer.

I have repeaters nested inside repeaters but if the first repeater is not visible the rest should follow.

aspx file

                    <h3> <%# DataBinder.Eval(Container.DataItem, "FullNm") %></h3>
                    <table ID="CollectorTable" runat="server" class="report-totals">
                        <tr>
                            <th>Total Collected:</th>
                            <td><asp:Literal ID="CollectorTotalCollected" runat="server" /></td>

                            <td class="report-totals-spacer"></td>

                            <th>Total Contacted:</th>
                            <td><asp:Literal ID="CollectorTotalContacted" runat="server" /></td>

                            <td class="report-totals-spacer"></td>

                            <th></th>
                            <td></td>
                        </tr>
                    </table>
               // etc....

Code Behind

        // ...pull totals
        Control CollectorRepeater = new Control();
        CollectorRepeater = (Control)e.Item.FindControl("CollectorRepeater");
        CollectorRepeater.Visible = false;

        Repeater collectorData = (Repeater)item.FindControl("CollectedTableRepeater");
        collectorData.DataSource = collectedDocuments;
        collectorData.DataBind();

        Repeater contactedData = (Repeater)item.FindControl("ContactedTableRepeater");
        contactedData.DataSource = contactedDocuments;
        contactedData.DataBind();

Solution

  • So all you need to do is check if your data is empty - either before you bind it, or on a repeater's OnDataBinding event, and hide the repeaters if appropriate.

    Repeater collectorData = (Repeater)item.FindControl("CollectedTableRepeater1");
    Repeater contactedData = (Repeater)item.FindControl("ContactedTableRepeater2");
    if( collectedDocuments.Tables[0].Rows.Count > 0 ){
            //if there is data(more than 0 rows), bind it
            collectorData.DataSource = collectedDocuments;
            collectorData.DataBind();
    
            contactedData.DataSource = contactedDocuments;
            contactedData.DataBind();
    } else {
            collectorData.Visible = False;
            //optional display "No data found" message
            contactedData.Visible = False;
    }