简体   繁体   中英

How can I create a table with dynamic columns using asp.net controls?

Background: My data model is a List<PersonDetail> , where PersonDetail contains properties like FirstName , LastName , etc, and the List can be any size.

I want to create a table that can compare these PersonDetails side-by-side. This means for each column I need to take the data in a single PersonDetail and spread it across my 8 rows. Then do the same for the next PersonDetail .

I have a constraint where I can only use an Asp.Net control ( ListView , GridView , Repeater , etc.) along with control.DataSource and control.DataBind() to create my table.

The template I want to do is something like:

<LayoutTemplate>
  <table>
    <tr>
      <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
    </tr>
    <tr>
      <asp:PlaceHolder runat="server" ID="itemPlaceHolder2"></asp:PlaceHolder>
    </tr>
    <!-- and so on -->
  </table>
</LayoutTemplate>

<ItemTemplate1>
  <td>
     <asp:Label ID="LabelFirstName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "FirstName")%>'></asp:Label>
  </td>
</ItemTemplate1>

<ItemTemplate2>
  <td>
     <asp:Label ID="LabelLastName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "LastName")%>'></asp:Label>
  </td> 
</ItemTemplate2>

<!-- and so on -->

Where the above ItemTemplates would be the only parts that repeat in the table.

I've tried all three control options and it doesn't seem to be supported without complicating the code-behind.

Is there a better data model I could use that can interface with another control? I've seen solutions like DataTable , but not sure if that will solve my problem..

Any ideas?

<table border="1">
    <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
            <tr>
                <td><%# DataBinder.Eval(Container.DataItem, "FirstName") %></td>
                <td><%# DataBinder.Eval(Container.DataItem, "LastName") %></td>
            </tr>
            <tr>
                <td><%# DataBinder.Eval(Container.DataItem, "Street") %></td>
                <td><%# DataBinder.Eval(Container.DataItem, "City") %></td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM