views:

23

answers:

3

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();
+1  A: 

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;
}
rlb.usa
This worked great. Did a little more on top of it to really make it what I wanted but this is the right idea.
Chris
A: 

In the code behind, in the Repeater's ItemCreated event you can do a check for the document count, and only bind the table within the repeater item it if the count for the data is more than 0.

ronaldwidha
A: 

You can do exactly like "rlb.usa" said or just replace the else part with:

else {
        collectorData.DataSource = null;
        collectorData.DataBind();

        contactedData.DataSource = null;
        contactedData.DataBind();
}
Shady M. Najib