简体   繁体   中英

Using Eval for creating Comma Separated string from Generic list

I have a Report class. The reports data is rendered using a repeater control. There is a department list (generic list of string) to be displayed as comma separated. Can we make it as a comma separated list in the repeater using Eval? If we cannot use Eval, is there any other syntax for this in repeater?

Class

 public class Report
{
    public int ReportID { get; set; }
    public string Title { get; set; }
    public string Recipients { get; set; }
    public string Frequency { get; set; }
    public List<string> DepartmentList { get; set; }

}

ASP.NET Markup

<asp:Repeater ID="rptReports" runat="server">
                    <HeaderTemplate>
                        <div>
                        </div>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <div class="repeaterIdentifier">
                            <div class="reportTitle">
                                <%# Eval("Title") +":" %>
                            </div>
                            <div class="reportFrequency">
                                <%# " Frequency - "+ Eval("Frequency") %>
                            </div>
                        </div>
                        <div class="reportContent">
                            <div class="repeaterLine">
                                <%# Eval("Recipients")%></div>
                        </div>
                    </ItemTemplate>
                </asp:Repeater>

您可以使用

<%#String.Join(",",((Report)Container.DataItem).DepartmentList)%>

Try this

<ItemTemplate>
   <%# String.Join((List<string>)Eval("DepartmentList")).ToArray()) %>
</ItemTemplate>

Note: code not tested

In your class Report , you may want to add an extra property

public string DepartmentListCommas
{
  get
  {
     return string.Join(", ", DepartmentList);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM