Search code examples
c#asp.netdata-controls

Generate datalist at runtime with fixed assigned templates


I have to generate DataLists at runtime according to the items selected by the user.

How can this be done from the .aspx code-behind? I have a fixed HeaderTemplate and ItemTemplate format for each of the DataList that will be generated.

<HeaderTemplate>
<tr>Table Heading<tr>
<tr>    <th>ItemNo</th> <th>Item Detail</th><tr>
</HeaderTemplate>

<ItemTemplate>
<tr>    <td><%#CallFunc()%></td>    <td><%#Eval("Item1")%></td> </tr>
<tr>    <td><%#CallFunc()%></td>    <td><%#Eval("Item2")%></td> </tr>
<tr>    <td><%#CallFunc()%></td>    <td><%#Eval("Item3")%></td> </tr>
<tr>    <td><%#CallFunc()%></td>    <td><%#Eval("Item4")%></td> </tr>
<tr>    <td><%#CallFunc()%></td>    <td><%#Eval("Item5")%></td> </tr>
<tr>    <td><%#CallFunc()%></td>    <td><%#Eval("Item6")%></td> </tr>
<tr>    <td><%#CallFunc()%></td>    <td><%#Eval("Item7")%></td> </tr>
<tr>    <td><%#CallFunc()%></td>    <td><%#Eval("Item8")%></td> </tr>
<tr>    <td><%#CallFunc()%></td>    <td><%#Eval("Item9")%></td> </tr>
<tr>    <td><%#CallFunc()%></td>    <td><%#Eval("Item10")%></td>    </tr>
<tr>    <td><%#CallFunc()%></td>    <td><%#Eval("Item11")%></td>    </tr>
</ItemTemplate>

A small example of this kind of runtime datalist generation, and assigning these templates to it would be greatly helpful.
Thanks


Solution

  • Why can not you use gridview?

    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="ItemID" HeaderText="Item ID" />
                <asp:BoundField DataField="ItemName" HeaderText="Item Name" />
            </Columns>
        </asp:GridView>