locked
Can we use Multiple Template Tags within repeater? RRS feed

  • Question

  • User-1027654633 posted

    Can we use Multiple Template Tags within repeater?

    <asp:Repeater id="Repeater" runat="server">

    <ItemTemplate>
    <%#DataBinder.Eval(Container.DataItem, "test1")%>
    </ItemTemplate>

    <AlternatingItemTemplate>
    <%#DataBinder.Eval(Container.DataItem, "test2")%>
    </AlternatingItemTemplate>

    <ItemTemplate>
    <%#DataBinder.Eval(Container.DataItem, "test3")%>
    </ItemTemplate>

    </asp:Repeater>
     
    If yes how to do ? If not then how can i achieve like the above ? 
    Saturday, April 20, 2019 11:42 AM

All replies

  • User475983607 posted

    You get one item template, one alternating item template, one header, and one footer.    The Repeater doc explains the server control and provide sample code.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.repeater.alternatingitemtemplate?view=netframework-4.8

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.repeater.itemtemplate?view=netframework-4.8

    Can you explain the design intent?  Why do you need 2 item templates?  Perhaps you need a nested Repeater?  Perhaps you can use one Item template?

    Saturday, April 20, 2019 1:11 PM
  • User-943250815 posted

    In addition to mgebhard.
    Have in mind that Container.DataItem represents one record/item from Datasource.
    In ItemTemplate and AlternateItemTemplate, you can layout presentation that best fits your needs, but still representing one record.
    Basically we use AlternateItemTemplate to have a different style of same layout, like a different background color.



    Saturday, April 20, 2019 2:31 PM
  • User288213138 posted

    Hi  About lokesh0443,
      You cannot use Multiple Template Tags in repeater, If you want to implement your own requirements, you can try nesting.

     

    The code:

    Hi  About lokesh0443,
      You cannot use Multiple Template Tags in repeater, If you want to implement your own requirements, you can try nesting.
    
    The code:
    
    <asp:Repeater ID="rptCustomers" runat="server" OnItemDataBound="OnItemDataBound">
        <HeaderTemplate>
            <table class="Grid" cellspacing="0" rules="all" border="1">
                <tr>
                    <th scope="col">
                        &nbsp;
                    </th>
                    <th scope="col" style="width: 150px">
                        Contact Name
                    </th>
                    <th scope="col" style="width: 150px">
                        City
                    </th>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <asp:Panel ID="pnlOrders" runat="server" Style="display: none">
                        <asp:Repeater ID="rptOrders" runat="server">
                            <HeaderTemplate>
                                <table class="ChildGrid" cellspacing="0" rules="all" border="1">
                                    <tr>
                                        <th scope="col" style="width: 150px">
                                            Order Id
                                        </th>
                                        <th scope="col" style="width: 150px">
                                            Date
                                        </th>
                                    </tr>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <tr>
                                    <td>
                                        <asp:Label ID="lblOrderId" runat="server" Text='<%# Eval("OrderId") %>' />
                                    </td>
                                    <td>
                                        <asp:Label ID="lblOrderDate" runat="server" Text='<%# Eval("OrderDate") %>' />
                                    </td>
                                </tr>
                            </ItemTemplate>
                            <FooterTemplate>
                                </table>
                            </FooterTemplate>
                        </asp:Repeater>
                    </asp:Panel>
                    <asp:HiddenField ID="hfCustomerId" runat="server" Value='<%# Eval("CustomerId") %>' />
                </td>
                <td>
                    <asp:Label ID="lblContactName" runat="server" Text='<%# Eval("ContactName") %>' />
                </td>
                <td>
                    <asp:Label ID="lblCity" runat="server" Text='<%# Eval("City") %>' />
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
    

    Best Regard,

    Sam

    Monday, April 22, 2019 11:19 AM