【问题标题】:How to show a message inside a Repeater control if it has no data inside it?如果Repeater控件内部没有数据,如何在其中显示消息?
【发布时间】:2012-03-06 05:18:28
【问题描述】:

我正在开发一个 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>

【问题讨论】:

标签: c# asp.net


【解决方案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;
            }
        }
    }
}

【讨论】:

  • 谢谢。非常感谢您的帮助。
  • 什么是rptDemo和repeaterTopItems?
  • rptDemo 是通过绑定数据触发此事件的中继器。 repeaterTopItems 实际上是一个类型,应该是:rptDemo,因为它指的是同一个中继器。
【解决方案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"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-28
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    相关资源
    最近更新 更多