3

I am developing an intranet web application. I am working now on the User Profile which shows four tables about the employee personal information, training courses, company short quizzes and his submitted ideas and suggestions.

What I want now is if the employee has no suggestions, the message such as ( You don't have any suggestions) inside the table instead of showing a table with its header without telling the user that he has no suggestions. So how to do that?

My ASP.NET code:

<asp:Repeater ID="Repeater4" runat="server" DataSourceID="SqlDataSource4">
                    <HeaderTemplate>
                        <div>
                        <table border="1">
                            <thead>
                                <tr>
                                    <td colspan="3">
                                        <center> <strong>Safety Suggestions</strong> </center>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <center> <strong>Suggestion Title</strong> </center>
                                    </td>
                                    <td>
                                        <center> <strong>Description</strong> </center>
                                    </td>
                                </tr>
                            </thead>

                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                                <p>
                                    <%# Eval("Title") %>
                                </p>
                            </td>
                            <td>
                                <p>
                                    <%# Eval("Description") %>
                                </p>
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                        </div>
                    </FooterTemplate>
                </asp:Repeater>
                <asp:SqlDataSource ID="SqlDataSource4" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT     dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsLog.Username
FROM         dbo.SafetySuggestionsLog INNER JOIN
                      dbo.employee ON dbo.SafetySuggestionsLog.Username = dbo.employee.Username
WHERE     (dbo.employee.Username = @Username)">
                    <SelectParameters>
                        <asp:Parameter Name="Username" />
                    </SelectParameters>
                </asp:SqlDataSource>
Ali Ahmed
  • 481
  • 3
  • 15
  • 28
  • Check out the answer to this question: http://stackoverflow.com/questions/6579814/render-empty-repeater – E.Z. Hart Mar 06 '12 at 05:22
  • Possible duplicate of [Default text for empty Repeater control](https://stackoverflow.com/questions/5271500/default-text-for-empty-repeater-control) – Jon Schneider May 30 '19 at 21:02

2 Answers2

14

You can use a footer template to manage massage, like this

step 1...

<FooterTemplate>
        <%-- Label used for showing Error Message --%>
        <asp:Label ID="lblErrorMsg" runat="server" Text="Sorry, no item is there to show." Visible="false">
        </asp:Label>
    </FooterTemplate>

step 2... handle visibility of lable in Repeater_ItemDataBound event like

protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    Repeater rptDemo = sender as Repeater; // Get the Repeater control object.

    // If the Repeater contains no data.
    if (repeaterTopItems != null && repeaterTopItems.Items.Count < 1)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            // Show the Error Label (if no data is present).
            Label lblErrorMsg = e.Item.FindControl("lblErrorMsg") as Label;
            if (lblErrorMsg != null)
            {
                lblErrorMsg.Visible = true;
            }
        }
    }
}
Verbeia
  • 4,400
  • 2
  • 23
  • 44
Mohit
  • 206
  • 3
  • 8
2
1. You can check for repeater items count in row databound event
2. Place a label somewhere in your repeater(footer etc.)
3. If count < 1 (find your label on footer row)
4. Populate label with "No data to display"
Zo Has
  • 12,599
  • 22
  • 87
  • 149