ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: ASP.NET :: ASP.NET - The DataList Control Êý¾ÝÁÐ±í¿Ø¼þ

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

ASP.NET
asp.netµÄ°²×°
ASP ºÍ ASP.NET Ö..
ASP.NET ½éÉÜ

ASP.NET - The DataList Control Êý¾ÝÁÐ±í¿Ø¼þ


×÷Õß:w3schools ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-08-09 ä¯ÀÀ:2851 :: ::

The DataList control is, like the Repeater control, used to display a repeated list of items that are bound to the control. However, the DataList control adds a table around the data items by default.
Êý¾ÝÁÐ±í¿Ø¼þ¸úת·¢Æ÷¿Ø¼þÀàËÆ£¬¶¼ÊÇÓÃÀ´ÏÔʾ°ó¶¨µ½¿Ø¼þÉÏÖØ¸´µÄÏîÄ¿ÁС£È»¶øÊý¾ÝÁÐ±í¿Ø¼þÔÚĬÈϵÄÇé¿öÏ»áÌî¼ÓÒ»¸öº¬ÓÐÊý¾ÝÏîµÄ±í¸ñ¡£


Examples ¾ÙÀý

DataList control

DataList control with styles [´øÑùʽµÄ]

DataList control with <AlternatingItemTemplate>


Bind a DataSet to a DataList Control
ΪÊý¾ÝÁÐ±í¿Ø¼þ°ó¶¨Êý¾Ý¼¯

The DataList control may be bound to a database table, an XML file, or another list of items. Here we will show how to bind an XML file to a DataList control.
Êý¾ÝÁÐ±í¿Ø¼þ¿ÉÒÔ°ó¶¨Êý¾Ý±í£¬XMLÎļþ£¬»òÊÇÆäËûµÄÏîÄ¿ÁС£ÏÖÔÚÎÒÃǽ«ÑÝʾÈçºÎ½«Ò»¸öXMLÎļþ°ó¶¨µ½Êý¾ÝÁÐ±í¿Ø¼þÉÏ¡£

We will use the following XML file in our examples ("cdcatalog.xml"):
ÎÒÃǵľÙÀýÖн«»áʹÓÃÏÂÃæµÄÕâ¸ö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>

Take a look at the XML file: cdcatalog.xml
Äã¿ÉÒÔÏȹ۲ìÒ»ÏÂÕâ¸öÎļþ£ºcdcatalog.xml

First, import the "System.Data" namespace. We need this namespace to work with DataSet objects. Include the following directive at the top of an .aspx page:
Ê×Ïȵ¼Èë "System.Data" ÃüÃû¿Õ¼ä¡£ÎÒÃÇÐèÒªÕâ¸öÃüÃû¿Õ¼ä¸úÊý¾Ý¼¯¶ÔÏóÒ»ÆðÔË×÷¡£ÔÚ.aspxÒ³ÃæµÄ¶¥²¿¼ÓÈëÒÔÏÂÖ¸Á

<%@ Import Namespace="System.Data" %>

Next, create a DataSet for the XML file and load the XML file into the DataSet when the page is first loaded:
½ÓÏÂÀ´£¬ÎªXMLÎļþ½¨Á¢Ò»¸öÊý¾Ý¼¯²¢µ±Ò³ÃæÔÚµÚÒ»´Î¼ÓÔØµÄʱºò½«XMLÔØÈëµ½Êý¾Ý¼¯ÖУº

<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 DataList in an .aspx page. 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:
È»ºóÎÒÃÇÔÚ.aspxÒ³Öн¨Á¢Ò»¸öÊý¾ÝÁÐ±í¡£<HeaderTemplate> ÔªËØÀïµÄÄÚÈÝÖ»»áÔÚÊä³öµÄʱºòÏÔʾһ´Î£¬È»ºó<ItemTemplate>ÔªËØÔ¶ÖØ¸´ÏÔʾÊý¾Ý¼¯ÀïÿÌõ“¼Ç¼”µÄÄÚÈÝ£¬×îºó <FooterTemplate> ÔªËØÀïµÄÄÚÈÝÒ²»áÔÚÊä³öµÄʱºòÏÔʾһ´Î£º

<html>
<body>
<form runat="server">
<asp:DataList id="cdcatalog" runat="server">
<HeaderTemplate>
...
</HeaderTemplate>
<ItemTemplate>
...
</ItemTemplate>
<FooterTemplate>
...
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>

Then we add the script that creates the DataSet and binds the mycdcatalog DataSet to the DataList control. We also fill the DataList control with a <HeaderTemplate> that contains the header of the table, an <ItemTemplate> that contains the data items to display, and a <FooterTemplate> that contains a text. Note that the gridlines attribute of the DataList is set to "both" to display table borders:
È»ºóÎÒÃǼÓÈë½Å±¾À´½¨Á¢Êý¾Ý¼¯£¬²¢½«Êý¾Ý¼¯mycdcatalog°ó¶¨µ½Êý¾ÝÁÐ±í¿Ø¼þÉÏ£¬ÎÒÃÇ»¹Ê¹ÓÃ<HeaderTemplate>À´°üº¬Á˱í¸ñµÄ±êÌ⣬Óà <ItemTemplate> À´ÏÔʾÊý¾ÝÌõÄ¿£¬²¢ÇÒÓÃ<FooterTemplate>À´¼ÓÈëÒ»¶ÎÎÄ×Ö¡£¿ÉÒÔ×¢Òâµ½ Êý¾ÝÁбíÖеÄgridlines ÊôÐÔ±»ÉèÖÃΪ"both"£¬ÕâÑù¿ÉÒÔÏÔʾ±í¸ñµÄ±ß¿ò£º

<%@ 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 Hege Refsnes
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>

 


Using Styles
ʹÓÃÑùʽ

You can also add styles to the DataList control to make the output more fancy:
Ä㻹¿ÉÒÔΪÊý¾Ý±í¿Ø¼þ¼ÓÈë¶à¸öÑùʽ£¬ÈÃÊä³öµÄÑù×Ó¸ü¼Ó³äÂúÏëÏó£º

<%@ 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 Hege Refsnes
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>

 


Using the <AlternatingItemTemplate>
ʹÓÃ<AlternatingItemTemplate>

You can add an <AlternatingItemTemplate> element after the <ItemTemplate> element to describe the appearance of alternating rows of output. You may style the data in the <AlternatingItemTemplate> section within the DataList control:
Äã¿ÉÒÔÔÚ<ItemTemplate>ÔªËØºó¼ÓÉÏÒ»¸ö <AlternatingItemTemplate>ÔªËØÀ´ÃèÊö½»²æÊä³öÐеÄÍâ¹Û¡£Äã¿ÉÒÔÔÚÊý¾ÝÁбíÖеÄ<AlternatingItemTemplate> ÇøÓòÀïÑùʽ»¯Êý¾Ý£º

<%@ 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>
&copy; Hege Refsnes
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>

ÆÀÂÛ (0) All