2009-03-23 3 views
35

List<AreaField>をリピータにバインドしようとしています。私はAreaField[]ジェネリックリストをリピータにバインドする -

の配列は、ここでのaspxで私のクラス階層

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

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

ToArray()メソッドを使用して、配列にリストを変換し、今持っている、私は(それをリピータをバインドするような何かをしたいと思いますthis)

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

MyAreaFieldName1は、AreaFieldItemクラスのNameプロパティの値です。

答えて

22

subRepeaterを作成することができます。

<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

+0

あなたのサブリピータコードはパーサエラーです。一重引用符でなければなりません:DataSource = '<%#Eval( "Fields")%>'。 –

62

をあなたのフィールドを書くことができますをキャストすることができます...

コードの後ろにコードがあります。

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

私はこれが役に立てば幸い!背後に

+6

これは正解とマークする必要があります。 – TheGeekZn

+0

PS:クラスはクラスの代わりに使用できます。 – dvdmn

+1

これは素晴らしいことです。ヘッダーとフッターのテンプレートは、テンプレート化されていない場合は必要ありません。あなたがリピータの前後に生のhtmlとしてこれらを置くならば、開閉HTMLをよりきれいに解析するでしょう。 – kns98

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の名前空間を忘れないでください)

例:

IList<Model> models = Builder<Model>.CreateListOfSize(10).Build(); 
List<Model> lstMOdels = models.ToList(); 
関連する問題