ASP.NET - Control Repeater

El control Repeater se utiliza para mostrar una lista de proyectos repetidos, que se limitan dentro del control.

Vincular DataSet a un control Repeater

El control Repeater se utiliza para mostrar una lista de proyectos repetidos, que se limitan dentro del control. El control Repeater puede estar vinculado a una tabla de base de datos, un archivo XML u otra lista de proyectos. Aquí, mostraremos cómo vincular un archivo XML a un control Repeater.

En este ejemplo, utilizaremos el siguiente archivo XML ("cdcatalog.xml"):

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
  <title>Empire Burlesque</title>
  <artist>Bob Dylan</artist>
  <country>USA</country>
  <company>Columbia</company>
  <price>10.90</price>
  <year>1985</year>
</cd>
<cd>
  <title>Hide your heart</title>
  <artist>Bonnie Tyler</artist>
  <country>UK</country>
  <company>CBS Records</company>
  <price>9.90</price>
  <year>1988</year>
</cd>
<cd>
  <title>Greatest Hits</title>
  <artist>Dolly Parton</artist>
  <country>USA</country>
  <company>RCA</company>
  <price>9.90</price>
  <year>1982</year>
</cd>
<cd>
  <title>Still got the blues</title>
  <artist>Gary Moore</artist>
  <country>UK</country>
  <company>Virgin records</company>
  <price>10.20</price>
  <year>1990</year>
</cd>
<cd>
  <title>Eros</title>
  <artist>Eros Ramazzotti</artist>
  <country>EU</country>
  <company>BMG</company>
  <price>9.90</price>
  <year>1997</year>
</cd>
</catalog>

Por favor, revise este archivo XML:cdcatalog.xml

Primero, importe el espacio de nombres "System.Data" .Necesitamos este espacio de nombres para trabajar con el objeto DataSet .En la parte superior de la página .aspx, incluya la siguiente instrucción:

<%@ Import Namespace="System.Data" %>

A continuación, creamos un DataSet para este archivo XML y cargamos este archivo XML en el DataSet cuando la página se carga por primera vez:

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycdcatalog=New DataSet
  mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
fin if
fin sub

Luego creamos un control Repeater en la página .aspx. El contenido del <HeaderTemplate> se muestra solo una vez en la salida, mientras que el contenido del <ItemTemplate> se repite en correspondencia con el "record" en el DataSet, y finalmente, el contenido del <FooterTemplate> se muestra solo una vez en la salida:

<html>
<body>
<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
...
</HeaderTemplate>
<ItemTemplate>
...
</ItemTemplate>
<FooterTemplate>
...
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>

Luego agregamos un script que crea un DataSet y este mycdcatalog DataSet se vincula al control Repeater. También llenamos este control Repeater con etiquetas HTML y usamos el método <%#Container.DataItem("fieldname")%> para vincular el elemento de datos al celda dentro del <ItemTemplate>:

<%@ Import Namespace="System.Data" %>
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycdcatalog=New DataSet
  mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
  cdcatalog.DataSource=mycdcatalog
  cdcatalog.DataBind()
fin if
fin sub
</script>
<html>
<body>
<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Título</th>
<th>Artista</th>
<th>País</th>
<th>Compañía</th>
<th>Precio</th>
<th>Año</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>

Mostrar este ejemplo

Se utiliza <AlternatingItemTemplate>

Se puede agregar un elemento <AlternatingItemTemplate> después del <ItemTemplate> para describir el aspecto de las filas alternas. En el siguiente ejemplo, cada fila de la tabla se muestra con un fondo de color gris claro:

<%@ Import Namespace="System.Data" %>
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycdcatalog=New DataSet
  mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
  cdcatalog.DataSource=mycdcatalog
  cdcatalog.DataBind()
fin if
fin sub
</script>
<html>
<body>
<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Título</th>
<th>Artista</th>
<th>País</th>
<th>Compañía</th>
<th>Precio</th>
<th>Año</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr bgcolor="#e8e8e8">
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>

Mostrar este ejemplo

Se utiliza <SeparatorTemplate>

El elemento <SeparatorTemplate> se puede utilizar para describir el separador entre cada registro. A continuación se muestra un ejemplo en el que se inserta una línea horizontal entre cada fila de la tabla:

<%@ Import Namespace="System.Data" %>
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycdcatalog=New DataSet
  mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
  cdcatalog.DataSource=mycdcatalog
  cdcatalog.DataBind()
fin if
fin sub
</script>
<html>
<body>
<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
<table border="0" width="100%">
<tr>
<th>Título</th>
<th>Artista</th>
<th>País</th>
<th>Compañía</th>
<th>Precio</th>
<th>Año</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td colspan="6"><hr /></td>
</tr>
</SeparatorTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>

Mostrar este ejemplo