Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在中继器中处理空数据_C#_Asp.net_Repeater - Fatal编程技术网

C# 在中继器中处理空数据

C# 在中继器中处理空数据,c#,asp.net,repeater,C#,Asp.net,Repeater,我已经尝试了很多教程,介绍了在中继器没有数据显示时,如何向用户显示标签。请有人能告诉我他们是否知道为什么标签不会显示?这是我的密码: <asp:Repeater ID="Notifications" runat="server" OnItemDataBound="Notifications_ItemDataBound"> <HeaderTemplate> <!-- directs us back to the previous page --

我已经尝试了很多教程,介绍了在中继器没有数据显示时,如何向用户显示标签。请有人能告诉我他们是否知道为什么标签不会显示?这是我的密码:

<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;
        }
    }
}
使用Repeater.Items.Count==0在页脚内为空时显示标签,并且不需要代码隐藏,如下所示:

<FooterTemplate>
    <asp:Label ID="lblEmptyData" runat="server" 
        Visible='<%# Notifications.Items.Count == 0 %>' Text="No Data To Display" />
</FooterTemplate>

检查如果e.Item.ItemType==ListItemType.Footer行触发了哪些项目?您是否询问如果reader.HasRows{/*…*/}或者{lblEmptyData.Visible=true}怎么办?@AndrewMorton我尝试过这个方法,但是它无法识别我的标签,并且错误显示“lblEmptyData的名称在当前上下文中不存在”??你能帮我解决这个问题吗?也许VS对页面上的内容感到困惑。我建议您从页面中删除标签,保存页面,然后使用稍微不同的名称创建新标签。它应该在代码隐藏中工作。为什么要将标签放在中继器中?中继器的工作是循环一组数据,并为每个项目显示一些重复的内容。没有数据时不显示消息,所以不要在其中嵌套空的数据标签。