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