Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 675
josephj1989 Avatar asked Feb 16 '11 01:02

josephj1989


2 Answers

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()

like image 92
Vadim Avatar answered Oct 20 '22 07:10

Vadim


Set the ItemType to System.String

<asp:Repeater ItemType="System.String" runat="server">     <ItemTemplate>         <%# Item %>     </ItemTemplate> </asp:Repeater> 
like image 31
Kevin Avatar answered Oct 20 '22 08:10

Kevin