2012-09-03 10 views
11

Supongamos que deseo vincular un tipo genérico (aquí: Dictionary<string, string>) a un repetidor utilizando el nuevo enlace de datos fuertemente tipado de ASP.NET 4.5.Encuadernación de datos fuertemente tipada y genéricos?

Entonces tendría que dejar KeyValuePair<string, string> como la Propiedad ItemType del Repetidor.

<asp:Repeater id="rpCategories" runat="server" ItemType="System.Collections.Generic.KeyValuePair<string, string>"> 

Existe un problema obvio aquí: no puedo usar < o > dentro del texto ItemType!

¿Cómo se puede lograr esto? ¿Es posible el uso de genéricos de alguna manera con el nuevo modelo de enlace de datos?

+0

Intentamos escapar con <y>? ¿Algún mensaje de error? – sisve

+0

No he intentado si eso funcionaría al ejecutar la página, pero VS lo marca como un error e Intellisense tampoco funciona. – magnattic

+0

No, no funciona tampoco. El mensaje de error es obviamente que VS no puede reconocer el tipo. – magnattic

Respuesta

12

Esto funciona para mí:

Código detrás

protected void Page_Load(object sender, EventArgs e) 
     { 
      rpCategories.DataSource = new Dictionary<string, string>() 
      { 
       {"1", "item"},{"2", "item"},{"3", "item"}, 
      }; 
     rpCategories.DataBind(); 
     } 

marcado

<asp:Repeater ID="rpCategories" runat="server" ItemType="System.Collections.Generic.KeyValuePair`2[System.String,System.String]"> 
     <ItemTemplate> 
      <asp:Label ID="Label1" runat="server" Text='<%# Item.Key %>'></asp:Label> 
     </ItemTemplate> 
    </asp:Repeater> 
+0

¡Gracias, eso funciona! – magnattic

+0

Esto resolvió un problema que tuve durante mucho tiempo, gracias. –

Cuestiones relacionadas