ASP.NET 中继器绑定列表<字符串> [英] ASP.NET Repeater bind List&lt;string&gt;

查看:32
本文介绍了ASP.NET 中继器绑定列表<字符串>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个 List 绑定到一个中继器控件.现在我想使用 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() %>

如果您担心空值,您可能需要重构为此(.NET 6+)

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>

请注意,如果您使用的版本低于 .NET 6,则不能使用 空条件运算符 Container.DataItem?.ToString()

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

这篇关于ASP.NET 中继器绑定列表<字符串>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆