ASP.NET - DataList control
- Vorige pagina WebForms Repeater
- Volgende pagina WebForms Databaseverbinding
The DataList control, similar to the Repeater control, is used to display a repeating list of items limited to the control. However, the DataList control will add a table by default to the data items.
Binding DataSet to DataList control
The DataList control, similar to the Repeater control, is used to display a repeating list of items limited to the control. However, the DataList control will add a table by default to the data items. The DataList control can be bound to a database table, an XML file, or other item lists. Here, we will show how to bind an XML file to a DataList control.
We will use the following XML file ("cdcatalog.xml") in the example:
<?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>
Bekijk dit XML-bestand:cdcatalog.xml
Eerst importeren we de "System.Data"-naamruimte. We hebben deze naamruimte nodig om samen te werken met de DataSet-object. Inclusief het volgende instructie in het begin van de .aspx-pagina:
<%@ Import Namespace="System.Data" %>
Maak vervolgens een DataSet aan voor deze XML-bestand en laad dit XML-bestand in de DataSet bij het eerste laden van de pagina:
<script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath("cdcatalog.xml")) end if end sub
Vervolgens maken we een DataList-knop aan in de .aspx-pagina. De inhoud van het <HeaderTemplate>-element verschijnt slechts eenmaal in de uitvoer, terwijl de inhoud van het <ItemTemplate>-element overeenkomt met de "record" in de DataSet en meerdere keren wordt herhaald. Ten slotte verschijnt de inhoud van het <FooterTemplate>-element slechts eenmaal in de uitvoer:
<html> <body> <form runat="server"> <asp:DataList id="cdcatalog" runat="server"> <HeaderTemplate> ... </HeaderTemplate> <ItemTemplate> ... </ItemTemplate> <FooterTemplate> ... </FooterTemplate> </asp:DataList> </form> </body> </html>
Vervolgens voegen we een script toe om een DataSet te creëren en binden we deze mycdcatalog DataSet aan de DataList-knop. We gebruiken deze elementen ook om de DataList-knop te vullen: het <HeaderTemplate> dat de kop bevat, het <ItemTemplate> dat de te tonen dataitems bevat, en het <FooterTemplate> dat de tekst bevat. Let op, de gridlines-eigenschap van de DataList is ingesteld op "both" om de tabelranden te tonen:
<%@ 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:DataList id="cdcatalog" gridlines="both" runat="server"> <HeaderTemplate> Mijn CD Catalogus </HeaderTemplate> <ItemTemplate> "<%#Container.DataItem("title")%>" van <%#Container.DataItem("artist")%> - $<%#Container.DataItem("price")%> </ItemTemplate> <FooterTemplate> Copyright codew3c.com </FooterTemplate> </asp:DataList> </form> </body> </html>
Gebruik stijlen
U kunt ook stijlen toevoegen aan de DataList-knop om hem fashionable te maken:
<%@ 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:DataList id="cdcatalog" runat="server" cellpadding="2" cellspacing="2" borderstyle="inset" backcolor="#e8e8e8" width="100%" headerstyle-font-name="Verdana" headerstyle-font-size="12pt" headerstyle-horizontalalign="center" headerstyle-font-bold="true" itemstyle-backcolor="#778899" itemstyle-forecolor="#ffffff" footerstyle-font-size="9pt" footerstyle-font-italic="true" <HeaderTemplate> Mijn CD Catalogus </HeaderTemplate> <ItemTemplate> "<%#Container.DataItem("title")%>" van <%#Container.DataItem("artist")%> - $<%#Container.DataItem("price")%> </ItemTemplate> <FooterTemplate> Copyright codew3c.com </FooterTemplate> </asp:DataList> </form> </body> </html>
Gebruik <AlternatingItemTemplate>
U kunt een <AlternatingItemTemplate> element toevoegen na het <ItemTemplate> element, zodat u het uiterlijk van alternatieve rijen kunt beschrijven. U kunt de <AlternatingItemTemplate> sectie van de DataList-knop styliseren binnen de DataList-knop:
<%@ 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:DataList id="cdcatalog" runat="server" cellpadding="2" cellspacing="2" borderstyle="inset" backcolor="#e8e8e8" width="100%" headerstyle-font-name="Verdana" headerstyle-font-size="12pt" headerstyle-horizontalalign="center" headerstyle-font-bold="True" itemstyle-backcolor="#778899" itemstyle-forecolor="#ffffff" alternatingitemstyle-backcolor="#e8e8e8" alternatingitemstyle-forecolor="#000000" footerstyle-font-size="9pt" footerstyle-font-italic="True"> <HeaderTemplate> Mijn CD Catalogus </HeaderTemplate> <ItemTemplate> "<%#Container.DataItem("title")%>" van <%#Container.DataItem("artist")%> - $<%#Container.DataItem("price")%> </ItemTemplate> <AlternatingItemTemplate> "<%#Container.DataItem("title")%>" van <%#Container.DataItem("artist")%> - $<%#Container.DataItem("price")%> </AlternatingItemTemplate> <FooterTemplate> © codew3c.com </FooterTemplate> </asp:DataList> </form> </body> </html>
- Vorige pagina WebForms Repeater
- Volgende pagina WebForms Databaseverbinding