contrôle ASP.NET Repeater
liste de contrôle Repeater est obligée de renouveler le programme de contrôle pour l'affichage.
DataSet Bind pour le contrôle Repeater
liste de contrôle Repeater est obligée de renouveler le programme de contrôle pour l'affichage. Le contrôle Repeater peut être lié à une table de base de données, fichier XML, ou d'une autre liste d'éléments. Ici, nous allons montrer comment lier un fichier XML à un contrôle Repeater.
Dans notre exemple, nous allons utiliser le document suivant XML ( "de cdcatalog.xml"):
<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>
Vérifiez le fichier XML: cdcatalog.xml
Tout d'abord, importer l'espace de noms "System.Data". Nous avons besoin de cet espace de noms pour travailler avec des objets DataSet. L'instruction suivante est incluse dans le haut de la page .aspx:
Ensuite, créez un DataSet pour le fichier XML, et quand la première page le chargement de ce fichier XML loading DataSet:
sub Page_Load
if Not Page.IsPostBack then
dim mycdcatalog=New DataSet
mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
end if
end sub
Ensuite, nous créons un contrôle Repeater dans une page .aspx. <HeaderTemplate> contenu de l'élément est rendu d'abord et une seule fois dans la sortie, et <ItemTemplate> contenu de l'élément correspondant à la DataSet chaque «dossier» est répété, et enfin, <FooterTemplate> contenu de l'élément sortie apparaît une seule fois:
<body>
<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
fr.
</HeaderTemplate>
<ItemTemplate>
fr.
</ItemTemplate>
<FooterTemplate>
fr.
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
Ensuite, nous ajoutons le script pour créer un DataSet, et se lie DataSet mycdcatalog le contrôle Repeater. Ensuite, utiliser des balises HTML pour remplir le contrôle Repeater, et par <% # Container.DataItem ( "fieldname")%> pour lier élément de données <ItemTemplate> zone cellulaire:
Exemples
<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>Title</th>
<th>Artist</th>
<th>Country</th>
<th>Company</th>
<th>Price</th>
<th>Year</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>
La démonstration >>
Utilisez <AlternatingItemTemplate>
Vous pouvez poster <ItemTemplate> élément <AlternatingItemTemplate> élément est utilisé pour décrire l'apparition de lignes de sortie alternatif. Dans l'exemple ci-dessous, le tableau sera affiché toutes les autres lignes comme un fond gris clair:
Exemples
<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>Title</th>
<th>Artist</th>
<th>Country</th>
<th>Company</th>
<th>Price</th>
<th>Year</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>
La démonstration >>
Utilisez <SeparatorTemplate>
<SeparatorTemplate> est utilisé pour décrire un séparateur entre chaque enregistrement. Dans l'exemple suivant, chaque table est insérée entre une ligne horizontale:
Exemples
<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>Title</th>
<th>Artist</th>
<th>Country</th>
<th>Company</th>
<th>Price</th>
<th>Year</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>
La démonstration >>