ASP.NET中继器绑定列表< string>

问题描述:

我将列表< string> 绑定到Repeater控件。现在我想使用 Eval 函数
ItemTemplate 中显示内容,如

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") %>.  

但我不知道我应该用而不是NAME。

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

只需使用<%#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>