重复列= 2时如何在Datalist中重复标题

时间:2013-08-28 11:53:46

标签: c# asp.net

我的要求是将DataList显示为

Header1      Header2          Header1      Header2           

1            Value 1            3          Value 3                        

2            Value 2            4          Value 4   

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

     <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>

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

2 个答案:

答案 0 :(得分:0)

添加&#39;如果datalistName.Items.Count&gt; 1&#39;代码示例:

    <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>

答案 1 :(得分:-1)

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

这样的事情:

<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, "");
            }
        }
    }
}

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