ASP.NET - Kontroler Repeater

Kontroler Repeater jest używany do wyświetlania listy powtarzających się elementów, które są ograniczone do tego kontrolera.

Powiązanie DataSet z kontrolerem Repeater

Kontrola Repeater jest używana do wyświetlania listy powtarzających się elementów, które są ograniczone do tego kontrolera. Kontrola Repeater może być powiązana z tabelą bazy danych, plikiem XML lub inną listą elementów. W tym miejscu pokażemy, jak powiązać plik XML z kontrolerem Repeater.

W przykładzie użyjemy poniższego pliku 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>

Proszę, sprawdź ten plik XML:cdcatalog.xml

Najpierw załaduj przestrzeń nazw "System.Data". Potrzebujemy tej przestrzeni nazw do pracy z obiektem DataSet. W górnej części strony .aspx zawiera poniższe polecenie:

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

Następnie utwórz DataSet dla tego pliku XML i załaduj ten plik XML do DataSet podczas pierwszego ładowania strony:

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

Następnie utwórz kontrolkę Repeater na stronie .aspx. Zawartość elementu <HeaderTemplate> pojawia się tylko raz w wyjściu, zawartość elementu <ItemTemplate> odpowiada powtarzającym się "record" w DataSet, a zawartość elementu <FooterTemplate> pojawia się tylko raz na końcu:

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

Następnie dodaj skrypt tworzący DataSet i przypisz ten DataSet mycdcatalog do kontrolki Repeater. Użyj znaczników HTML do wypełnienia kontrolki Repeater i powiąż dane za pomocą metody <%#Container.DataItem("fieldname")%> w sekcji <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()
end if
end sub
</script>
<html>
<body>
<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Tytuł</th>
<th>Artysta</th>
<th>Kraj</th>
<th>Firma</th>
<th>Cena</th>
<th>Rok</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>

Wyświetl ten przykład

Użyj <AlternatingItemTemplate>

Możesz dodać element <AlternatingItemTemplate> po <ItemTemplate>, aby opisać wygląd przeplatanych wierszy. W poniższym przykładzie każda para wierszy tabeli będzie miała szary tło:

<%@ 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()
end if
end sub
</script>
<html>
<body>
<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Tytuł</th>
<th>Artysta</th>
<th>Kraj</th>
<th>Firma</th>
<th>Cena</th>
<th>Rok</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>

Wyświetl ten przykład

Użyj <SeparatorTemplate>

Element <SeparatorTemplate> może być używany do opisu separatora między rekordami. Poniższy przykład umieszcza poziomą linię między wierszami tabeli:

<%@ 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()
end if
end sub
</script>
<html>
<body>
<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
<table border="0" width="100%">
<tr>
<th>Tytuł</th>
<th>Artysta</th>
<th>Kraj</th>
<th>Firma</th>
<th>Cena</th>
<th>Rok</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>

Wyświetl ten przykład