ASP.NET - La commande de répéteur |
Pour montrer une liste répétée d'articles qui sont liés à la commande, la commande de répéteur est employée.
|
Lier un ensemble de données à une commande de répéteur
|
Pour montrer une liste répétée d'articles qui sont liés à la commande, la commande de répéteur est employée. La commande de répéteur peut être liée à un dossier de XML, à une table de base de données, ou à une liste différente d'articles. Ici nous te montrerons comment lier un dossier de XML à une commande de répéteur.
Nous emploierons le dossier suivant de XML dans nos exemples ("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>
|
|
Au début, importer le namespace de « System.Data » et nous avons besoin de ce namespace pour fonctionner avec des objets d'ensemble de données. Puis, au dessus d'une page de .aspx inclure la directive suivante :
|
<%@ Import Namespace="System.Data" %>
|
|
Après, créer un ensemble de données pour le dossier de XML et quand la page est d'abord chargée, charger le dossier de XML dans l'ensemble de données :
|
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycdcatalog=New DataSet
mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
end if
end sub
|
|
Then we create a Repeater control in an .aspx page and the contents of the <HeaderTemplate> element are rendered first and only once within the output,then the contents of the <ItemTemplate> element are repeated for each "record" in the DataSet, and last,
the contents of the <FooterTemplate> element are rendered once within the output:
|
<html>
<body>
<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
...
</HeaderTemplate>
<ItemTemplate>
...
</ItemTemplate>
<FooterTemplate>
...
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
|
|
Then we add the script that creates the DataSet and then binds
the mycdcatalog DataSet to the Repeater control.With HTML tags,we also fill
the Repeater control and bind the data items to
the cells in the<ItemTemplate> section with the <%#Container.DataItem("fieldname")%>
method:
|
<%@ 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>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>
|
|
Using the <AlternatingItemTemplate>
|
After the <ItemTemplate> element,you can add an <AlternatingItemTemplate> element to describe the appearance of alternating rows of output.In the example below each other row in the table will be displayed in a light grey color:
|
<%@ 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>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>
|
|
Using the <SeparatorTemplate>
|
To describe a separator between each record,the <SeparatorTemplate>element can be used.
The example below inserts a horizontal line between each table row:
|
<%@ 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>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>
|
|
 |
 |
Keywords:
ASP.NET using the Repeater Control,
asp net repeater,
vb net repeater,
asp net control,
asp net using,
vb net control,
visual basic using,
visual basic control,
datagrid repeater,
repeater database,
repeater c#,
repeater dropdownlist,
repeater edit,
wireless repeater,
c# using,
repeater hyperlink,
repeater paging,
javascript repeater,
repeater viewstate,
datagrid control,
custom repeater,
repeater checkbox,
repeater databind,
repeater datalist,
data repeater,
dataset repeater,
dynamic repeater,
datalist control,
repeater textbox,
control data,
repeater button,
listbox control,
c# control,
checkbox control,
using msdn,
event control,
repeater vb,
textbox control
|