josephj1989 josephj1989 - 1 month ago 19
ASP.NET (C#) Question

ASP.NET Repeater bind List<string>

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.

Answer

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

If you are worried about null values you may want to refactor to this

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