ASP.NET Repeater bind List

2019-01-13 09:14发布

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.

7条回答
Luminary・发光体
2楼-- · 2019-01-13 09:24

A more complete example based on the LINQ provided by @RobertoBr:

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>
查看更多
劫难
3楼-- · 2019-01-13 09:28

This should work just fine:

<ItemTemplate>
   <%=this.GetDataItem().ToString() %>
</ItemTemplate>
查看更多
三岁会撩人
4楼-- · 2019-01-13 09:35
rptSample.DataSource = from c in lstSample select new { NAME = c };

in the repeater you put

<%# Eval("NAME") %>
查看更多
霸刀☆藐视天下
5楼-- · 2019-01-13 09:44

Set the ItemType to System.string

<asp:Repeater ItemType="System.string" runat="server">
    <ItemTemplate>
        <%# Item %>
    </ItemTemplate>
</asp:Repeater>
查看更多
Anthone
6楼-- · 2019-01-13 09:44

you have to use the databing syntax here or it will not work.

<%# this.GetDataItem().ToString() %>
查看更多
Juvenile、少年°
7楼-- · 2019-01-13 09:44

Inside Item Template

     <ItemTemplate>
 <asp:Label ID="lblName"  runat="server" Text='<%# Eval("YourEntityName").ToString() ==""? "NA" : Eval("YourEntityName").ToString()%>'></asp:Label>
    <ItemTemplate>

or Simply Add inside Item Template

<%# Eval("YourEntityName").ToString() ==""? "NA" : Eval("YourEntityName").ToString()%>
查看更多
登录 后发表回答