C# 将通用列表绑定到中继器-ASP.NET

C# 将通用列表绑定到中继器-ASP.NET,c#,asp.net,repeater,C#,Asp.net,Repeater,我正在尝试将列表绑定到中继器。我已经使用ToArray()方法将列表转换为一个数组,现在有了一个AreaField[] 这是我的类层次结构 public class AreaFields { public List<Fields> Fields { set; get; } } public class Fields { public string Name { set; get; } public string Value {set; get; } } M

我正在尝试将
列表
绑定到中继器。我已经使用
ToArray()
方法将列表转换为一个数组,现在有了一个
AreaField[]

这是我的类层次结构

public class AreaFields
{
    public List<Fields> Fields { set; get; }
}

public class Fields
{
    public string Name { set; get; }
    public string Value {set; get; }
}

MyAreaFieldName1是AreaFieldItem类中Name属性的值。

您可能需要创建一个子转发器

<asp:Repeater ID="SubRepeater" runat="server" DataSource='<%# Eval("Fields") %>'>
  <ItemTemplate>
    <span><%# Eval("Name") %></span>
  </ItemTemplate>
</asp:Repeater>

你也可以投你的场

<%# ((ArrayFields)Container.DataItem).Fields[0].Name %>

最后,您可以使用CSV函数编写字段

<%# GetAsCsv(((ArrayFields)Container.DataItem).Fields) %>

public string GetAsCsv(IEnumerable<Fields> fields)
{
  var builder = new StringBuilder();
  foreach(var f in fields)
  {
    builder.Append(f);
    builder.Append(",");
  }
  builder.Remove(builder.Length - 1);
  return builder.ToString();
}

公共字符串GetAsCsv(IEnumerable字段)
{
var builder=新的StringBuilder();
foreach(字段中的变量f)
{
附加(f);
生成器。追加(“,”);
}
builder.Remove(builder.Length-1);
返回builder.ToString();
}

它非常简单

代码隐藏:

// Here's your object that you'll create a list of
private class Products
{
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    public string ProductPrice { get; set; }
}

// Here you pass in the List of Products
private void BindItemsInCart(List<Products> ListOfSelectedProducts)
{   
    // The the LIST as the DataSource
    this.rptItemsInCart.DataSource = ListOfSelectedProducts;

    // Then bind the repeater
    // The public properties become the columns of your repeater
    this.rptItemsInCart.DataBind();
}
//这是您要创建的对象列表
私人类产品
{
公共字符串ProductName{get;set;}
公共字符串ProductDescription{get;set;}
公共字符串ProductPrice{get;set;}
}
//在这里您可以传递产品列表
私有void BindItemsInCart(所选产品列表)
{   
//将列表作为数据源
this.rptItemsInCart.DataSource=所选产品列表;
//然后绑定中继器
//公共属性将成为中继器的列
this.rptItemsInCart.DataBind();
}
ASPX代码:

<asp:Repeater ID="rptItemsInCart" runat="server">
  <HeaderTemplate>
    <table>
      <thead>
        <tr>
            <th>Product Name</th>
            <th>Product Description</th>
            <th>Product Price</th>
        </tr>
      </thead>
      <tbody>
  </HeaderTemplate>
  <ItemTemplate>
    <tr>
      <td><%# Eval("ProductName") %></td>
      <td><%# Eval("ProductDescription")%></td>
      <td><%# Eval("ProductPrice")%></td>
    </tr>
  </ItemTemplate>
  <FooterTemplate>
    </tbody>
    </table>
  </FooterTemplate>
</asp:Repeater>

品名
产品说明
产品价格
我希望这有帮助

代码隐藏:

public class Friends
{
    public string   ID      { get; set; }
    public string   Name    { get; set; }
    public string   Image   { get; set; }
}

protected void Page_Load(object sender, EventArgs e)
{
        List <Friends> friendsList = new List<Friends>();

        foreach (var friend  in friendz)
        {
            friendsList.Add(
                new Friends { ID = friend.id, Name = friend.name }    
            );

        }

        this.rptFriends.DataSource = friendsList;
        this.rptFriends.DataBind();
}
公共课好友
{
公共字符串ID{get;set;}
公共字符串名称{get;set;}
公共字符串图像{get;set;}
}
受保护的无效页面加载(对象发送方、事件参数e)
{
列表好友列表=新列表();
foreach(friendz中的var friend)
{
好友列表。添加(
新朋友{ID=friend.ID,Name=friend.Name}
);
}
this.rptFriends.DataSource=friendsList;
this.rptFriends.DataBind();
}

.aspx页

<asp:Repeater ID="rptFriends" runat="server">
            <HeaderTemplate>
                <table border="0" cellpadding="0" cellspacing="0">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Name</th>
                        </tr>
                    </thead>
                    <tbody>
            </HeaderTemplate>
            <ItemTemplate>
                    <tr>
                        <td><%# Eval("ID") %></td>
                        <td><%# Eval("Name") %></td>
                    </tr>
            </ItemTemplate>
            <FooterTemplate>
                    </tbody>
                </table>
            </FooterTemplate>
        </asp:Repeater>

身份证件
名称
您应该使用ToList()方法。 (不要忘记System.Linq命名空间)

例:

IList models=Builder.CreateListOfSize(10.Build();
List lstMOdels=models.ToList();

runat=“子中继器”?!?--这违背了我对runat属性的所有理解(或我认为我理解)。你的子转发器代码给出了一个解析器错误,应该是单引号:DataSource=''。PS:struct可以用来代替class。这很好。请注意,如果页眉和页脚未模板化,则不需要它们。如果你把它们作为原始html放在转发器之前和之后,会使打开和关闭html的解析更加清晰。如果你忘了将类产品中的变量公开,那么它将不起作用。我犯了那个错误。
<asp:Repeater ID="rptFriends" runat="server">
            <HeaderTemplate>
                <table border="0" cellpadding="0" cellspacing="0">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Name</th>
                        </tr>
                    </thead>
                    <tbody>
            </HeaderTemplate>
            <ItemTemplate>
                    <tr>
                        <td><%# Eval("ID") %></td>
                        <td><%# Eval("Name") %></td>
                    </tr>
            </ItemTemplate>
            <FooterTemplate>
                    </tbody>
                </table>
            </FooterTemplate>
        </asp:Repeater>
IList<Model> models = Builder<Model>.CreateListOfSize(10).Build();
List<Model> lstMOdels = models.ToList();