2009-03-23 32 views
35

Estoy tratando de vincular un List<AreaField> a un repetidor. He convertido la lista en una matriz utilizando el método ToArray() y ahora tienen una variedad de AreaField[]Enlazando una lista genérica a un repetidor - ASP.NET

Aquí está mi jerarquía de clases

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

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

En el aspx, me gustaría para obligar a éste un repetidor (algo así como este)

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

el MyAreaFieldName1 es el valor de la propiedad Nombre de la clase AreaFieldItem.

Respuesta

22

Es posible que desee crear un subRepeater.

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

También se puede convertir sus campos

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

Finalmente se podía hacer un poco de Función CSV y escribir sus campos con una función

<%# 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"?!? - Eso desafía todo lo que entiendo (o creo que entiendo) sobre el atributo runat. – Hardryv

+0

Su código sub-repetidor tal como está da un error de analizador, debe ser comillas simples: DataSource = '<% # Eval ("Fields")%>'. –

62

Es sorprendentemente simple ...

Código detrás de:

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

código 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> 

espero que esto ayude!

+6

Esto debe marcarse como la respuesta correcta. – TheGeekZn

+0

PS: struct se puede usar en lugar de la clase. – dvdmn

+1

Esto es genial. Tenga en cuenta que la plantilla de encabezado y pie de página no es necesaria si no tienen plantillas. Hará que la apertura y el cierre de html analicen más limpiamente si los coloca como html sin formato antes y después del repetidor. – kns98

6

código subyacente:

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

página .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

Debe utilizar el método ToList(). (No se olvide de System.Linq espacio de nombres)

ej .:

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