处理Repeater中的空数据

问题描述 投票:0回答:1

当没有数据要从转发器显示时,我尝试过很多关于尝试向用户显示标签的教程。请!谁能告诉我他们是否知道标签为什么不显示?这是我的代码:

<asp:Repeater ID="Notifications" runat="server" OnItemDataBound="Notifications_ItemDataBound">
    <HeaderTemplate>
        <!-- directs us back to the previous page -->
        <asp:LinkButton ID="linkReturn" runat="server" >
            <span aria-hidden="true" style="color:black; font-size: xx-large !important;" class="glyphicon glyphicon-log-out"></span>
        </asp:LinkButton>
        <h1>Notifications</h1>
        <hr />
    </HeaderTemplate>
    <ItemTemplate>
        <div class="repeater-border">
        <b><u>Problem Number:</u></b>&nbsp;<%# Eval("Problem_Id")%><br />
        <b><u>Tenant Name:</u></b>&nbsp;<%# Eval("Tenant_FullName")%><br />
        <b><u>Property Address:</u></b>&nbsp;<%# Eval("Property_Address")%><br />
        <b><u>Message:</u></b>&nbsp;<%# Eval("Message")%><br />
        <!-- link that allows landlords to delete the tenant from that property -->
        <asp:HyperLink ID="DeleteLink" NavigateUrl='<%#Eval("Problem_Id","DeleteProblem.aspx?id={0}") %>' runat="server"  CssClass="btn btn-danger btn-sm">Delete</asp:HyperLink>
        </div>
    </ItemTemplate>
    <SeparatorTemplate>
        <hr />
    </SeparatorTemplate>
    <FooterTemplate>
    <!-- Label used for showing Error Message -->
    <asp:Label ID="lblEmptyData" Text="No Data To Display" runat="server" Visible="false"> </asp:Label>
    </FooterTemplate>
</asp:Repeater>

代码背后:

string checkLandlord = String.Empty;
protected void Page_Load(object sender, EventArgs e)
{
    checkLandlord += Session["LandlordLogin"];

    if (!Page.IsPostBack)
    {
        //Creating a connection to my database using the connection string
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["rent-dbConnectionString1"].ToString();
        SqlCommand comm = new SqlCommand();
        //preparing a query which will select all properties matching the landlord that is logged in at that moment
        comm.CommandText = "select prob.Message, prop.Property_Address, t.Tenant_FullName, t.Tenant_Email from Properties prop join Tenants t on prop.Property_Id = t.Property_Id  join Problems prob on prop.Property_Id = prob.Property_Id and prob.Tenant_Id = t.Tenant_Id join Landlords l on prop.Landlord_Id = l.Landlord_Id where l.Landlord_Email ='" + checkLandlord + "'";
        comm.Connection = con;
        SqlDataReader reader;
        try
        {
            con.Open();
            reader = comm.ExecuteReader();
            if (reader.HasRows)
            {
                Notifications.DataSource = reader;
                Notifications.DataBind();
            }
            reader.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
        finally
        {
            con.Close();
        }

    }
}
protected void Notifications_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (Notifications.Items.Count < 1)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            Label lblFooter = (Label)e.Item.FindControl("lblEmptyData");
            lblFooter.Visible = true;
        }
    }
}
c# asp.net repeater
1个回答
1
投票

使用Repeater.Items.Count == 0在内部空白时显示Label,并且不需要代码隐藏,如下所示:

<FooterTemplate>
    <asp:Label ID="lblEmptyData" runat="server" 
        Visible='<%# Notifications.Items.Count == 0 %>' Text="No Data To Display" />
</FooterTemplate>
© www.soinside.com 2019 - 2024. All rights reserved.