如果里面没有数据,如何在Repeater控件内显示一条消息?

时间:2012-03-06 05:18:29

标签: c# asp.net

我正在开发一个Intranet Web应用程序。我现在正在使用用户档案,其中显示了关于员工个人信息,培训课程,公司简短测验以及他提交的想法和建议的四个表格。

我现在想要的是,如果员工没有任何建议,表内的信息(例如您没有任何建议),而不是显示带有标题的表而不告诉用户他没有建议。 那怎么做?

我的ASP.NET代码:

<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>

2 个答案:

答案 0 :(得分:14)

您可以使用页脚模板管理按摩,例如

第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>

第2步...... 处理Repeater_ItemDataBound事件中的标签的可见性,如

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;
            }
        }
    }
}

答案 1 :(得分: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"