ASP.NET - Repeater-kontrolli
- Edellinen sivu WebForms XML tiedosto
- Seuraava sivu WebForms DataList
Repeater-kontrolli näyttää toistuvia kohteiden luetteloja, jotka on rajattu kyseisessä kontrollissa.
Esimerkki
DataSetin yhdistäminen Repeater-kontrolliin
Repeater-kontrolli näyttää toistuvia kohteiden luetteloja, jotka on rajattu kyseisessä kontrollissa. Repeater-kontrolli voidaan yhdistää tietokantatauluihin, XML-tiedostoihin tai muihin kohteiden luetteloihin. Tässä näytämme, miten XML-tiedosto voidaan yhdistää Repeater-kontrolliin.
Esimerkissä käytämme seuraavaa XML-tiedostoa ("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>
Tarkista seuraava XML-tiedosto:cdcatalog.xml
Ensimmäiseksi, tuodaan "System.Data"-niminen tila. Tarvitsemme tätä tilaa työskennelläksemme DataSet-objektin kanssa. Lisää seuraava ohje .aspx-sivun ylätunnisteeseen:
<%@ Import Namespace="System.Data" %>
Luo sitten DataSet XML-tiedostolle ja lue tämä XML-tiedosto DataSetiin sivun ensimmäisellä latauksella:
<script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath("cdcatalog.xml")) end if end sub
Lisäämme sitten Repeater-kontrollin .aspx-sivulle. <HeaderTemplate> -elementin sisältö näkyy vain kerran tulosteessa, <ItemTemplate> -elementin sisältö vastaa "record"-tietueen toistoa DataSetissä, ja viimeinen <FooterTemplate> -elementin sisältö näkyy vain kerran tulosteessa:
<html> <body> <form runat="server"> <asp:Repeater id="cdcatalog" runat="server"> <HeaderTemplate> ... </HeaderTemplate> <ItemTemplate> ... </ItemTemplate> <FooterTemplate> ... </FooterTemplate> </asp:Repeater> </form> </body> </html>
Lisäämme sitten skriptin, joka luo DataSetin ja sitoo tämän mycdcatalog DataSetin Repeater-kontrolliin. Käytämme myös HTML-merkkejä täyttääksemme Repeater-kontrollin ja sidottaan tietojoukon <%#Container.DataItem("fieldname")%> -menetelmällä soluihin <ItemTemplate> -osassa:
<%@ 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>
Käytä <AlternatingItemTemplate>
Voit lisätä <AlternatingItemTemplate> -elementin <ItemTemplate> -elementin jälkeen, jotta voit kuvata vaihtelevien rivien ulkoasun. Seuraavassa esimerkissä taulukon joka toinen rivi näkyy vaaleanharmaana taustavärillä:
<%@ 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>
Käytä <SeparatorTemplate>
<SeparatorTemplate> -elementti voidaan käyttää kuvaamaan eri tietueiden välisiä erotinmerkkejä. Seuraava esimerkki lisää horisontaalisen viivan jokaisen taulukon rivin väliin:
<%@ 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>
- Edellinen sivu WebForms XML tiedosto
- Seuraava sivu WebForms DataList