Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically set control IDs inside a repeater template?

Here is a perplexing issue I have not seen a good answer to on StackOverflow, although there a couple stabs at it... I have a situation where I'd like to do this:

<asp:Repeater ID="MyRepeater" runat="server" OnItemDataBound="MyRepeater_ItemDataBound">
    <ItemTemplate>
        <li id="id?">
            All the other stuff
        </li>
    </ItemTemplate>
</asp:Repeater>

The question... is how do I get the ID of my <li> elements to be id1, id2, id3, etc., based on the ItemIndex they are bound to? So far the most... er..."elegant" solution I've come up with is to replace the <li> with an asp:Literal and dump the <li...>' text. But that just feels... so wrong. And no, I'm not using ASP.NET 4.0, which I've read will provide this functionality.

like image 517
Bryan Avatar asked Apr 27 '10 00:04

Bryan


People also ask

How many inline templates are used in Repeater control?

A Repeater has five inline templates to format it: <Itemtemplate> - It defines how the each item is rendered from the Data Source collection.

Which template is a much when displaying the data in a Repeater control?

HeaderTemplate: This template is used for elements that you want to render once before your ItemTemplate section.


1 Answers

Like this:

<asp:Repeater ID="MyRepeater" runat="server" OnItemDataBound="MyRepeater_ItemDataBound">
    <ItemTemplate>
        <li id="li<%# ((RepeaterItem)Container).ItemIndex + 1%>">
            All the other stuff
        </li>
    </ItemTemplate>
</asp:Repeater>
like image 107
SLaks Avatar answered Sep 22 '22 22:09

SLaks