ASP.NET - Repeater ¿Ø¼þ

Repeater ¿Ø¼þÓÃÓÚÏÔÊ¾ÖØ¸´µÄÏîÄ¿ÁÐ±í£¬ÕâЩÏîÄ¿±»ÏÞÖÆÔڸÿؼþ¡£

°Ñ DataSet °ó¶¨µ½ Repeater ¿Ø¼þ

Repeater ¿Ø¼þÓÃÓÚÏÔÊ¾ÖØ¸´µÄÏîÄ¿ÁÐ±í£¬ÕâЩÏîÄ¿±»ÏÞÖÆÔڸÿؼþ¡£Repeater ¿Ø¼þ¿É±»°ó¶¨µ½Êý¾Ý¿â±í¡¢XML Îļþ»òÕ߯äËûÏîÄ¿ÁÐ±í¡£ÕâÀÎÒÃǽ«Õ¹Ê¾ÈçºÎ°Ñ XML Îļþ°ó¶¨µ½Ò»¸ö Repeater ¿Ø¼þ¡£

ÎÒÃǽ«ÔÚÀý×ÓÖÐʹÓÃÏÂÃæµÄ XML Îļþ("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>

Çë²é¿´¸Ã XML Îļþ£ºcdcatalog.xml

Ê×ÏÈ£¬µ¼Èë "System.Data" ÃüÃû¿Õ¼ä¡£ÎÒÃÇÐèÒª´ËÃüÃû¿Õ¼äÓë DataSet ¶ÔÏóһͬ¹¤×÷¡£ÔÚ .aspx Ò³ÃæµÄ¶¥²¿°üº¬ÏÂÃæÕâÌõÖ¸Á

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

½ÓÏÂÀ´£¬ÎªÕâ¸ö XML Îļþ´´½¨Ò»¸ö DataSet£¬²¢°Ñ´Ë XML ÎļþÔÚÒ³ÃæÊ״μÓÔØÊ±ÔØÈë DataSet£º

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
  dim mycdcatalog=New DataSet
  mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
end if
end sub

È»ºóÎÒÃÇÔÚ .aspx Ò³ÃæÖд´½¨Ò»¸ö Repeater ¿Ø¼þ¡£<HeaderTemplate> ÔªËØÖеÄÄÚÈÝÔÚÊä³öÖнö³öÏÖÒ»´Î£¬¶ø <ItemTemplate> ÔªËØµÄÄÚÈÝ»á¶ÔÓ¦ DataSet ÖÐµÄ "record" ÖØ¸´³öÏÖ£¬×îºó£¬<FooterTemplate> µÄÄÚÈÝÔÚÊä³öÖнö³öÏÖÒ»´Î£º

<html>
<body>

<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">

<HeaderTemplate>
...
</HeaderTemplate>

<ItemTemplate>
...
</ItemTemplate>

<FooterTemplate>
...
</FooterTemplate>

</asp:Repeater>
</form>

</body>
</html>

È»ºóÎÒÃÇÌí¼Ó¿É´´½¨ DataSet µÄ½Å±¾£¬²¢°ÑÕâ¸ö mycdcatalog DataSet °ó¶¨µ½ Repeater ¿Ø¼þ¡£ÎÒÃÇͬÑùÓà HTML ±êÇ©À´Ìî³äÕâ¸ö Repeater ¿Ø¼þ£¬²¢Í¨¹ý <%#Container.DataItem("fieldname")%> ·½·¨°ÑÊý¾ÝÏîÄ¿°ó¶¨µ½ <ItemTemplate> ²¿·ÖÄڵĵ¥Ôª¸ñ£º

<%@ 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>

ÏÔʾÕâ¸öÀý×Ó

ʹÓà <AlternatingItemTemplate>

Äú¿ÉÒÔÔÚ <ItemTemplate> ÔªËØºóÌí¼Ó <AlternatingItemTemplate> ÔªËØ£¬ÕâÑù¾Í¿ÉÒÔÃèÊö½»ÌæÐеÄÍâ¹ÛÁË¡£ÔÚÏÂÃæµÄÀý×ÓÖУ¬¸Ã±í¸ñÖÐÿ¸ôÒ»ÐоͻáÏÔʾΪdz»ÒÉ«µÄ±³¾°£º

<%@ 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>

ÏÔʾÕâ¸öÀý×Ó

ʹÓà <SeparatorTemplate>

<SeparatorTemplate> ÔªËØÄܹ»ÓÃÓÚÃèÊöÿ¸ö¼Ç¼֮¼äµÄ·Ö¸ô·û¡£ÏÂÃæµÄÀý×ÓÔÚÿ¸ö±í¸ñÐÐÖ®¼ä²åÈëÁËÒ»ÌõˮƽÏߣº

<%@ 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>

ÏÔʾÕâ¸öÀý×Ó