如何使用asp:asp中的表:ListView LayoutTemplate ItemTemplate

时间:2012-11-29 15:41:10

标签: c# asp.net listview

当我调用ListView的DataBind()方法

时,我收到以下异常消息

'Table' cannot have children of type 'ListViewDataItem'.

这是我的标记

<asp:ListView ID="lv" runat="server">
    <LayoutTemplate>
        <asp:Table ID="t" runat="server" CssClass="contentframework">
            <asp:TableHeaderRow>
                <asp:TableHeaderCell>1</asp:TableHeaderCell>
                <asp:TableHeaderCell>2</asp:TableHeaderCell>
            </asp:TableHeaderRow>

            <asp:TableRow ID="itemPlaceHolder" runat="server" />

        </asp:Table>


    </LayoutTemplate>

    <ItemTemplate>
        <asp:TableRow>
            <asp:TableCell><%# Eval("1") %></asp:TableCell>
            <asp:TableCell><%# Eval("2") %></asp:TableCell>
        </asp:TableRow>
    </ItemTemplate>

    <EmptyDataTemplate>
        <h2>No Records!</h2>
    </EmptyDataTemplate>
</asp:ListView>

3 个答案:

答案 0 :(得分:5)

Asp.net服务器端控件有一些hierarchy。所以它不允许ListViewDataItem作为asp:Table元素的子元素。

所以,最好是尝试简单的html表标签而不是服务器标签,如:

<asp:ListView ID="lv" runat="server">
    <LayoutTemplate>
        <table id="t" class="contentframework">
            <thead>
                <th>
                    1
                </th>
                <th>
                    2
                </th>
            </thead>
           <tr id="itemPlaceholder" runat="server"></tr>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <%# Eval("1") %>
            </td>
            <td>
                <%# Eval("2") %>
            </td>
        </tr>
    </ItemTemplate>
    <EmptyDataTemplate>
        <h2>
            No Records!</h2>
    </EmptyDataTemplate>
</asp:ListView>

作为列表视图的参考,您可以查看this文章。

更多详情:

您正面临异常,因为ListViewDataItem类表示ListView控件中的单个数据项(即DataBoundControl),因此它不能直接作为Table类的子控件,但它可以是子类HTML表格显然直接嵌入到html标签中(如上面的代码所示)。

答案 1 :(得分:0)

试试这个:

<LayoutTemplate>
    <asp:Table ID="t" runat="server" CssClass="contentframework">
        <asp:TableHeaderRow>
            <asp:TableHeaderCell>1</asp:TableHeaderCell>
            <asp:TableHeaderCell>2</asp:TableHeaderCell>
        </asp:TableHeaderRow>

        <asp:PlaceHolder id="itemPlaceholder" runat="server" />

    </asp:Table>


</LayoutTemplate>

这应该允许列表项填充为实际行。

答案 2 :(得分:0)

你必须在asp:TableRow中嵌套一个asp:tableCell,然后占位符应该在tablecell中工作

df1 <- structure(list(ClientID = c("ABC", "XYZ", "PQW"), Reason_1 = c("Trend", 
 "", ""), Reason_2 = c("", "High", ""), Reason_3 = c("Low", "Low", 
 ""), Reason_4 = c("", "", "High"), Reason_5 = c("High", "", "Low"
 ), Reason_6 = c("Med", "", ""), Reason_7 = c("", "Low", ""), 
Reason_8 = c("", "", "Med")), .Names = c("ClientID", "Reason_1", 
"Reason_2", "Reason_3", "Reason_4", "Reason_5", "Reason_6", "Reason_7", 
"Reason_8"), class = "data.frame", row.names = c(NA, -3L))