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.
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()
Set the ItemType to System.String
<asp:Repeater ItemType="System.String" runat="server"> <ItemTemplate> <%# Item %> </ItemTemplate> </asp:Repeater>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With