ASP.NET Repeater bind List

前端 未结 7 414
遇见更好的自我
遇见更好的自我 2020-12-04 20:50

I am binding a List to a Repeater control. Now I want to use the Eval function to display the contents in ItemTemplate

相关标签:
7条回答
  • 2020-12-04 21:03
    rptSample.DataSource = from c in lstSample select new { NAME = c };
    

    in the repeater you put

    <%# Eval("NAME") %>
    
    0 讨论(0)
  • 2020-12-04 21:08

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

    <%# this.GetDataItem().ToString() %>
    
    0 讨论(0)
  • 2020-12-04 21:14

    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()%>
    
    0 讨论(0)
  • 2020-12-04 21:20

    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>
    
    0 讨论(0)
  • 2020-12-04 21:21

    Set the ItemType to System.string

    <asp:Repeater ItemType="System.string" runat="server">
        <ItemTemplate>
            <%# Item %>
        </ItemTemplate>
    </asp:Repeater>
    
    0 讨论(0)
  • 2020-12-04 21:23

    Just use <%# Container.DataItem.ToString() %>

    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>
    

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

    0 讨论(0)
提交回复
热议问题