ASP.NET - DataList-kontrolli
- Edellinen sivu WebForms Repeater
- Seuraava sivu WebForms -tietokantayhteys
DataList-kontrolli, joka on samankaltainen Repeater-kontrollin kanssa, näyttää toistuvan listan projekteja, jotka on rajoitettu kyseiseen kontrolliin. Kuitenkin DataList-kontrolli lisää oletuksena taulukon tietoitempiin.
Esimerkki
Yhdistä DataSet DataList-kontrolliin
DataList-kontrolli, joka on samankaltainen Repeater-kontrollin kanssa, näyttää toistuvan listan projekteja, jotka on rajoitettu kyseiseen kontrolliin. Kuitenkin DataList-kontrolli lisää oletuksena taulukon tietoitempiin. DataList-kontrolli voidaan yhdistää tietokantatauluun, XML-tiedostoon tai muihin projektin listoihin. Tässä näytämme, miten XML-tiedosto voidaan yhdistää DataList-kontrolliin.
Käytämme esimerkissä 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 tuomme "System.Data"-nimenkutsun. Tarvitsemme tätä nimenkutsua työskennellessämme DataSet-objektin kanssa. Lisää seuraava ohje .aspx-sivun ylähyllylle:
<%@ Import Namespace="System.Data" %>
Luo seuraavaksi DataSet XML-tiedostolle ja lue XML-tiedosto DataSetiin sivun latautuessa:
<script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath("cdcatalog.xml")) end if end sub
Sitten luomme DataList-kontrollin .aspx-sivulla. <HeaderTemplate>-elementin sisältö näkyy vain kerran tulosteessa, kun taas <ItemTemplate>-elementin sisältö vastaa "record"-tietokannan toistoa, ja viimein <FooterTemplate>-elementin sisältö näkyy vain kerran tulosteessa:
<html> <body> <form runat="server"> <asp:DataList id="cdcatalog" runat="server"> <HeaderTemplate> ... </HeaderTemplate> <ItemTemplate> ... </ItemTemplate> <FooterTemplate> ... </FooterTemplate> </asp:DataList> </form> </body> </html>
Sitten lisäämme skriptin, joka luo DataSetin, ja sidomme tämän mycdcatalog DataSetin DataList -kontrolliin. Käytämme näitä elementtejä myös DataList -kontrollin täyttämiseen: sisältää otsikon <HeaderTemplate>, sisältää näytettävät tietueet <ItemTemplate> ja sisältää tekstin <FooterTemplate>. Huomaa, että DataListin gridlines-ominaisuus on asetettu arvoon "both", jotta näytetään taulukon reunat:
<%@ 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> My CD Catalog </HeaderTemplate> <ItemTemplate> "<%#Container.DataItem("title")%>" of <%#Container.DataItem("artist")%> - $<%#Container.DataItem("price")%> </ItemTemplate> <FooterTemplate> Copyright codew3c.com </FooterTemplate> </asp:DataList> </form> </body> </html>
Käytä tyylejä
Voit myös lisätä tyylejä DataList -kontrolliin, jotta se näyttää tyylikkäältä:
<%@ 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> My CD Catalog </HeaderTemplate> <ItemTemplate> "<%#Container.DataItem("title")%>" of <%#Container.DataItem("artist")%> - $<%#Container.DataItem("price")%> </ItemTemplate> <FooterTemplate> Copyright codew3c.com </FooterTemplate> </asp:DataList> </form> </body> </html>
Käytä <AlternatingItemTemplate>
Voit lisätä <ItemTemplate> -elementin jälkeen <AlternatingItemTemplate> -elementin, jotta voit kuvata vaihtelevien rivien ulkoasua. Voit tyylittää <AlternatingItemTemplate> -osaa DataList -kontrollin sisällä:
<%@ 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> My CD Catalog </HeaderTemplate> <ItemTemplate> "<%#Container.DataItem("title")%>" of <%#Container.DataItem("artist")%> - $<%#Container.DataItem("price")%> </ItemTemplate> <AlternatingItemTemplate> "<%#Container.DataItem("title")%>" of <%#Container.DataItem("artist")%> - $<%#Container.DataItem("price")%> </AlternatingItemTemplate> <FooterTemplate> © codew3c.com </FooterTemplate> </asp:DataList> </form> </body> </html>
- Edellinen sivu WebForms Repeater
- Seuraava sivu WebForms -tietokantayhteys