将一个泛型列表绑定到重复器 - ASP.NET

36
我正在尝试将一个List<AreaField>绑定到一个重复器上。我使用ToArray()方法将列表转换为数组,现在有一个AreaField[]数组。
这是我的类层次结构。
public class AreaFields
{
    public List<Fields> Fields { set; get; }
}

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

在 aspx 中,我想将其绑定到重复器(类似于此)。

DataBinder.Eval(Container.DataItem, "MyAreaFieldName1")

MyAreaFieldName1是AreaFieldItem类中Name属性的值。

4个回答

65

令人惊讶的是,这很简单...

代码如下:

// 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();
}

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>

希望这能有所帮助!


PS:可以使用struct代替class。 - dvdmn
2
这很棒。请注意,如果Header和Footer模板没有被模板化,则不需要它们。如果您将这些内容作为重复器之前和之后的原始HTML放置,将使打开和关闭HTML解析更加清晰。 - kns98
如果你忘记将类“Products”中的变量设置为公共变量,那么它就无法工作。我曾经犯过这个错误。 - Jon

23

你可能想创建一个子重复器。

<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();
}

4
runat="SubRepeater"?!? -- 这违背了我对 runat 属性的理解(或者说是我认为的理解)。 - Hardryv
你的子重复器代码存在解析器错误,应该使用单引号:DataSource='<%# Eval("Fields") %>'。 - Ian Grainger

6

代码后台:

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();
}

.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>

-1
你应该使用ToList()方法。(别忘了System.Linq命名空间)

ex.:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接