0

我的要求是显示 DataList 如下

Header1      Header2          Header1      Header2           

1            Value 1            3          Value 3                        

2            Value 2            4          Value 4   

但我无法重复数据列表的标题部分。我的数据列表的完整代码是

     <asp:DataList ID="datalstProfileCount" runat="server" RepeatColumns="2" RepeatDirection="Horizontal"
                        RepeatLayout="Table" ShowHeader="true" OnItemDataBound="datalstProfileCount_ItemDataBound">
                        <HeaderTemplate>
                            <table border="0" cellpadding="0" cellspacing="0" bordercolor="#e1e1e0" style="border: solid 1px #e1e1e0;
                                font-family: Segoe UI; font-size: 12px; font-weight: bold; width: 400px">
                                <tr style="font-size: 13px; background: #ffedc2; border-bottom: 1px solid #eba602;
                                    border-left: 1px solid #d6d6d6; font-weight: 600; font-size: 13px; padding: 10px 8px;
                                    color: #c82124;">
                                    <td style="width: 80px; color: #c84241; text-align: center; padding: 6px">
                                        Name
                                    </td>
                                    <td style="width: 100px; color: #c84241; text-align: center; padding: 6px">
                                       Total Count
                                    </td>
                                    <td style="width: 100px; color: #c84241; text-align: center; padding: 6px">
                                        Active Members Count
                                    </td>
                                </tr>
                            </table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <table border="0" cellpadding="0" cellspacing="0" bordercolor="#e1e1e0" style="border: solid 1px #e1e1e0;
                                font-family: Segoe UI; font-size: 12px; font-weight: bold; width: 400px; margin: -1px">
                                <tr style="font-size: 12px; vertical-align: middle; color: #9d9d9c;">
                                    <td style="text-align: left; width: 80px; color: #333333; padding: 6px">
                                        <asp:Label ID="Label2" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
                                    </td>
                                    <td style="text-align: right; width: 100px; color: #333333; padding: 6px">
                                        <asp:Label ID="lblTotalCount" runat="server" Text='<%#Eval("TotalCount") %>'></asp:Label>
                                    </td>
                                    <td style="text-align: right; width: 100px; color: #333333; padding: 6px">
                                        <asp:Label ID="lblActiveMembersCount" runat="server" Text='<%#Eval("ActiveMembersCount") %>'></asp:Label>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                        <FooterTemplate>
                            <table border="0" cellpadding="0" cellspacing="0" style="padding-top: 20px" align="center">
                                <tr>
                                    <td style="font-size: 14px">
                                        Total Count:
                                    </td>
                                    <td style="padding-left: 10px">
                                        <asp:Label ID="lblProfileCount" runat="server" Text="" ForeColor="#C82124" Font-Size="14px"></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="font-size: 14px">
                                        Active Members Count:
                                    </td>
                                    <td style="padding-left: 10px">
                                        <asp:Label ID="lblMemberCount" runat="server" Text="" ForeColor="#C82124" Font-Size="14px"></asp:Label>
                                    </td>
                                </tr>
                            </table>
                        </FooterTemplate>
                    </asp:DataList>

请你们中的任何人帮助我..提前谢谢你...

4

2 回答 2

0

添加 'If datalistName.Items.Count > 1' 代码示例:

    <HeaderTemplate>
          <table>
              <tr><td>Header1</td><td>Header2</td></tr>
                 <%If (data_list_name.Items.Count() > 1) {%>
                   <tr><td>Header1</td><td>Header2</td></tr>
                 <%}%>
          </table>
    </HeaderTemplate>
于 2018-01-02T09:33:57.070 回答
-1

Header 永远不会重复,将 Header HTML 作为 Item Template 的一部分。根据 .Net Controls 实现,页眉是列表的头,只会出现一次,然后是项目模板,它将根据数据源中存在的项目数重复,然后是页脚。

像这样的东西:

<asp:DataList ID="datalstProfileCount" runat="server" RepeatColumns="2" RepeatDirection="Horizontal"   RepeatLayout="Table" ShowHeader="true" OnItemDataBound="datalstProfileCount_ItemDataBound">
          <HeaderTemplate>         
          </HeaderTemplate>
          <ItemTemplate>
                    //Header Section
                    //Data 
          </ItemTemplate>
</asp:DataList>

如果您仍然有一些困惑,请与您在评论中提到的标题部分和数据部分分享您的整个数据列表,我将为您提供更新的代码..

更新答案:

<asp:DataList ID="datalstProfileCount" runat="server" RepeatColumns="2" RepeatDirection="Horizontal"
        RepeatLayout="Table" ShowHeader="true" OnItemDataBound="datalstProfileCount_ItemDataBound">
        <HeaderTemplate>
        </HeaderTemplate>
        <ItemTemplate>
            <table border="0" cellpadding="0" cellspacing="0" bordercolor="#e1e1e0" style="border: solid 1px #e1e1e0;
                font-family: Segoe UI; font-size: 12px; font-weight: bold; width: 400px; margin: -1px">
                <tr style="font-size: 13px; background: #ffedc2; border-bottom: 1px solid #eba602;
                    border-left: 1px solid #d6d6d6; font-weight: 600; font-size: 13px; padding: 10px 8px;
                    color: #c82124; visibility: hidden;" id="trHeader" runat="server">
                    <td style="width: 80px; color: #c84241; text-align: center; padding: 6px">
                        Name
                    </td>
                    <td style="width: 100px; color: #c84241; text-align: center; padding: 6px">
                        Total Count
                    </td>
                    <td style="width: 100px; color: #c84241; text-align: center; padding: 6px">
                        Active Members Count
                    </td>
                </tr>
                <tr style="font-size: 12px; vertical-align: middle; color: #9d9d9c;">
                    <td style="text-align: left; color: #333333; padding: 6px">
                        <asp:Label ID="Label2" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
                    </td>
                    <td style="text-align: right; color: #333333; padding: 6px">
                        <asp:Label ID="lblTotalCount" runat="server" Text='<%#Eval("TotalCount") %>'></asp:Label>
                    </td>
                    <td style="text-align: right; color: #333333; padding: 6px">
                        <asp:Label ID="lblActiveMembersCount" runat="server" Text='<%#Eval("ActiveMembersCount") %>'></asp:Label>
                    </td>
                </tr>
            </table>
        </ItemTemplate>
        <FooterTemplate>
            <table border="0" cellpadding="0" cellspacing="0" style="padding-top: 20px" align="center">
                            <tr>
                                <td style="font-size: 14px">
                                    Total Count:
                                </td>
                                <td style="padding-left: 10px">
                                    <asp:Label ID="lblProfileCount" runat="server" Text="" ForeColor="#C82124" Font-Size="14px"></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td style="font-size: 14px">
                                    Active Members Count:
                                </td>
                                <td style="padding-left: 10px">
                                    <asp:Label ID="lblMemberCount" runat="server" Text="" ForeColor="#C82124" Font-Size="14px"></asp:Label>
                                </td>
                            </tr>
            </table>
        </FooterTemplate>
</asp:DataList>

在服务器端添加以下代码:

protected void datalstProfileCount_ItemDataBound(Object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        if(e.Item.ItemIndex <=1)
        {
            var headerRow = (System.Web.UI.HtmlControls.HtmlTableRow)e.Item.FindControl("trHeader");
            if(headerRow != null)
            {
                headerRow.Style.Add(HtmlTextWriterStyle.Visibility, "");
            }
        }
    }
}

您可能需要对样式进行一些更改,但这也可以实现。

于 2013-08-28T11:56:24.067 回答