我将List<string>
绑定到Repeater控件。现在我想使用Eval
函数来显示ItemTemplate
中的内容
<%# Eval("NAME") %>.
但我不确定我应该使用什么而不是NAME。
只需使用<%# Container.DataItem.ToString() %>
如果您担心空值,可能需要重构(.NET 6+)
<asp:Repeater ID="repeater" runat="server">
<ItemTemplate>
<%# Container.DataItem?.ToString() ?? string.Empty%>
</ItemTemplate>
</asp:Repeater>
请注意,如果使用的是少于.NET 6,则无法使用null-conditional operator Container.DataItem?.ToString()
将ItemType设置为System.string
<asp:Repeater ItemType="System.string" runat="server">
<ItemTemplate>
<%# Item %>
</ItemTemplate>
</asp:Repeater>
rptSample.DataSource = from c in lstSample select new { NAME = c };
在你转发的中继器
<%# Eval("NAME") %>
这应该工作得很好:
<ItemTemplate>
<%=this.GetDataItem().ToString() %>
</ItemTemplate>
你必须在这里使用数据处理语法,否则它将无法工作。
<%# this.GetDataItem().ToString() %>
基于@RobertoBr提供的LINQ的更完整示例:
代码背后:
List<string> notes = new List<string>();
notes.Add("Value1")
notes.Add("Value2")
repeaterControl1.DataSource = from c in notes select new {NAME = c};
repeaterControl1.DataBind();
在页面上:
<asp:Repeater ID="repeaterControl1" runat="server" >
<ItemTemplate>
<li><%# Eval("NAME") %></li>
</ItemTemplate>
</asp:Repeater>
内部项目模板
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("YourEntityName").ToString() ==""? "NA" : Eval("YourEntityName").ToString()%>'></asp:Label>
<ItemTemplate>
或者只是添加内部项目模板
<%# Eval("YourEntityName").ToString() ==""? "NA" : Eval("YourEntityName").ToString()%>