如果儿童中继器为空,如何显示中继器的子页脚?

时间:2017-09-07 11:23:33

标签: asp.net

我的应用程序中嵌套了两个中继器,工作正常。如果儿童中继器是空的,我很想显示页脚。由于我的代码是我的长html,我将删除我的html的样本并发布我的完整代码以便更好地理解。一切正常,但除非子转发器为空,我想显示页脚消息

<asp:Repeater ID="ProductRepeater" runat="server"           OnItemDataBound="ProductRepeater_ItemDataBound">
    <HeaderTemplate></HeaderTemplate>
    <ItemTemplate>
    <table>
    <tr>
      <td>
        <%#Eval("Name")%>
      </td>
    <tr>
    </table>
    <pre>
    <asp:Repeater ID="ChildRepeater" runat="server">
      <HeaderTemplate></HeaderTemplate>
        <ItemTemplate>
        <table>
          <tr>
            <td>
              <%#Eval("Description")%>
            <td>
          </tr>
        <table>
        </itemTemplate>
      <FooterTemplate>
      <div>
        <h5>
          <asp:Label ID="lblDefaultMessage" runat="server" Text="This is empty. " Visible="false" ForeColor="Red" Font-Size="Large">
          </asp:Label>
        </h5>
      </div>
    </FooterTemplate>
    </asp:Repeater>
    </pre>
    </ItemTemplate>
  </asp:Repeater>


    <protected void ProductRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        string category = "Value"
        Repeater rptRelated = e.Item.FindControl("ChildRepeater") as Repeater;
        rptRelated.DataSource = getChild(category);
        rptRelated.DataBind();
        if (rptRelated.Items.Count < 1)
        {
            if (e.Item.ItemType == ListItemType.Footer)
            {
                Label lblDefaultMessage = (Label)e.Item.FindControl("lblDefaultMessage");
                lblDefaultMessage.Visible = true;

            } 
        }

    }
}

2 个答案:

答案 0 :(得分:0)

查找中继器,如:

// find Child Repeater
Control ctrl = (sender) as Control;
Repeater rptRelated = ctrl.Parent.NamingContainer as Repeater;

从Repeater的Footer中找到控件,如:

Label lblDefaultMessage = 
  (Label)rptRelated.Controls[rptRelated.Controls.Count-1].FindControl("lblDefaultMessage");

将其添加到子级Repeater的OnItemDataBound事件中,而不是父级。

protected void ChildRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    Control ctrl = (sender) as Control;
    Repeater rptRelated = ctrl.Parent.NamingContainer as Repeater;
    if (rptRelated.Items.Count < 1)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            Label lblDefaultMessage = (Label)rptRelated.Controls[rptRelated.Controls.Count-1].FindControl("lblDefaultMessage");
            lblDefaultMessage.Visible = true;
        }
    }
}

活动是:

<asp:Repeater ID="ChildRepeater" runat="server" 
              OnItemDataBound="ChildRepeater_ItemDataBound">

答案 1 :(得分:0)

转发器没有 EmptyDataTemplate ,例如 GridView 。 您应将页脚内容放在 Repeater 旁边,然后更改“数据绑定”代码,如下所示:

Repeater rptRelated = e.Item.FindControl("ChildRepeater") as Repeater;
Label lblDefaultMessage = (Label)e.Item.FindControl("lblDefaultMessage");    
var ds = getChild(category);
lblDefaultMessage.Visible = ds != null && ds.Rows.Count != 0;
if (rptRelated != null)
{
  rptRelated.DataSource = ds;
  rptRelated.DataBind();
}