ASP.NET - The Repeater Control

The Repeater control is used to display a repeated list of items that are bound to the control.


Examples

1.Repeater control

<%@ 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>Company</th>
<th>Price</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%> </td>
<td><%#Container.DataItem("artist")%> </td>
<td><%#Container.DataItem("company")%> </td>
<td><%#Container.DataItem("price")%> </td>
</tr>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>
</form>

</html>
</body>

output result:

Title Artist Company Price
Empire Burlesque  Bob Dylan  Columbia  10.90 
Hide your heart  Bonnie Tyler  CBS Records  9.90 
Greatest Hits  Dolly Parton  RCA  9.90 
Still got the blues  Gary Moore  Virgin records  10.20 
Eros  Eros Ramazzotti  BMG  9.90 

2.Repeater control with <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:Repeater id="cdcatalog" runat="server">

<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Title</th>
<th>Artist</th>
<th>Company</th>
<th>Price</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%> </td>
<td><%#Container.DataItem("artist")%> </td>
<td><%#Container.DataItem("company")%> </td>
<td><%#Container.DataItem("price")%> </td>
</tr>
</ItemTemplate>

<AlternatingItemTemplate>
<tr bgcolor="#e8e8e8">
<td><%#Container.DataItem("title")%> </td>
<td><%#Container.DataItem("artist")%> </td>
<td><%#Container.DataItem("company")%> </td>
<td><%#Container.DataItem("price")%> </td>
</tr>
</AlternatingItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>
</form>

</html>
</body>

output result:

Title Artist Company Price
Empire Burlesque  Bob Dylan  Columbia  10.90 
Hide your heart  Bonnie Tyler  CBS Records  9.90 
Greatest Hits  Dolly Parton  RCA  9.90 
Still got the blues  Gary Moore  Virgin records  10.20 
Eros  Eros Ramazzotti  BMG  9.90 

3.Repeater control with <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 align="left">Title</th>
<th align="left">Artist</th>
<th align="left">Company</th>
<th align="left">Price</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%> </td>
<td><%#Container.DataItem("artist")%> </td>
<td><%#Container.DataItem("company")%> </td>
<td><%#Container.DataItem("price")%> </td>
</tr>
</ItemTemplate>

<SeparatorTemplate>
<tr>
<td colspan="6"><hr /></td>
</tr>
</SeparatorTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>
</form>

</html>
</body>

output result:
Title Artist Company Price
Empire Burlesque  Bob Dylan  Columbia  10.90 

Hide your heart  Bonnie Tyler  CBS Records  9.90 

Greatest Hits  Dolly Parton  RCA  9.90 

Still got the blues  Gary Moore  Virgin records  10.20 

Eros  Eros Ramazzotti  BMG  9.90 



Bind a DataSet to a Repeater Control

The Repeater control is used to display a repeated list of items that are bound to the control. The Repeater 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 Repeater control.

We will use the following XML file in our examples ("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>

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:

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

<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 Repeater control 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:

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

Then we add the script that creates the DataSet and binds the mycdcatalog DataSet to the Repeater control. We also fill the Repeater control with HTML tags and bind the data items to the cells in the<ItemTemplate> section with the <%#Container.DataItem("fieldname")%> method:

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


Using the <AlternatingItemTemplate>

You can add an <AlternatingItemTemplate> element after the <ItemTemplate> element to describe the appearance of alternating rows of output. In the following example each other row in the table will be displayed in a light grey color:

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


Using the <SeparatorTemplate>

The <SeparatorTemplate> element can be used to describe a separator between each record. The following example inserts a horizontal line between each table row:

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


posted on 2007-03-06 15:47  改变热爱  阅读(294)  评论(0)    收藏  举报

导航