ASP.NET Repeater bind List<string>

90,632

Solution 1

Just use <%# Container.DataItem.ToString() %>

If you are worried about null values you may want to refactor to this (.NET 6+)

<asp:Repeater ID="repeater" runat="server">
    <ItemTemplate>
        <%# Container.DataItem?.ToString() ?? string.Empty%>
    </ItemTemplate>
</asp:Repeater>

Note if you are using less than .NET 6 you cannot use the null-conditional operator Container.DataItem?.ToString()

Solution 2

Set the ItemType to System.String

<asp:Repeater ItemType="System.String" runat="server">
    <ItemTemplate>
        <%# Item %>
    </ItemTemplate>
</asp:Repeater>

Solution 3

rptSample.DataSource = from c in lstSample select new { NAME = c };

in the repeater you put

<%# Eval("NAME") %>

Solution 4

This should work just fine:

<ItemTemplate>
   <%=this.GetDataItem().ToString() %>
</ItemTemplate>

Solution 5

you have to use the databind syntax here or it will not work.

<%# this.GetDataItem().ToString() %>
Share:
90,632

Related videos on Youtube

josephj1989
Author by

josephj1989

I am a software enthusiast mainly into .NET , C# , ASP.NET , Jquery, Oracle , SQL Server , Java and Oracle Ebusiness Suite. I have many years of experience in the industry and really enjoy solving problems.

Updated on April 08, 2021

Comments

  • josephj1989
    josephj1989 about 3 years

    I am binding a List<string> to a Repeater control. Now I want to use the Eval function to display the contents in ItemTemplate like

    <%# Eval("NAME") %>.  
    

    But I am not sure what I should use instead of NAME.

  • Matthew Lock
    Matthew Lock about 11 years
    Some examples use <%# DataBinder.Eval(Container.DataItem, "NAME") %> instead of simply using Eval. What's the difference?
  • Jonathan van de Veen
    Jonathan van de Veen over 10 years
    Note that the ItemType property is introduced in .NET Framework 4.5.