【问题标题】:Binding a generic list to a repeater - ASP.NET将通用列表绑定到转发器 - ASP.NET
【发布时间】:2009-03-23 16:53:46
【问题描述】:

我正在尝试将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 属性的值。

【问题讨论】:

    标签: c# asp.net repeater


    【解决方案1】:

    这出乎意料的简单......

    后面的代码:

    // 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。
    • 这很棒。请注意,如果没有模板化,则不需要 Header 和 Footer 模板。如果您将这些作为原始 html 放在转发器之前和之后,将使打开和关闭 html 解析更干净。
    • 如果您忘记将 Products 类中的变量公开,那么它将无法工作。我犯了那个错误。
    【解决方案2】:

    您可能想要创建一个子中继器。

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

    【讨论】:

    • runat="SubRepeater"?!? -- 这违背了我对 runat 属性的所有理解(或认为我理解)。
    • 您的子中继器代码原样给出解析器错误,应该是单引号:DataSource=''。
    【解决方案3】:

    代码背后:

    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>
    

    【讨论】:

      【解决方案4】:

      你应该使用 ToList() 方法。 (不要忘记 System.Linq 命名空间)

      例如:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多