Instance
-
Repeater control
-
Repeater control with <alternatingitemtemplate>
-
With the <separatortemplate> Repeater control
Bind dataset to the Repeater control
The repeater space is used to display the list of repeated projects, which are restricted in this control. The Repeater control can be bound to database tables, XML files, or other project lists. Here, we will show how to bind an XML file to a repeater control.
In this example, we will use the following XML file ("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> <title> hide your heart </title> <artist> Bonnie Taylor </ 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> <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> <title> Eros </title> <artist> Eros ramazzotti </artist> <country> Eu </country> <company> BMG </company> <price> 9.90 </price> <year> 1997 </year> </Cd> </CATALOG>
Please view the XML file: cdcatalog. xml
First, import the "system. Data" namespace. We need this namespace to work with the DataSet object. At the top of the. ASPX page, include the following command:
<% @ Import namespace = "system. Data" %>
Next, create a dataset for the XML file and load the XML file into the dataset when the page is loaded for the first time:
<SCRIPT runat = "server"> sub page_loadif not page. ispostback then dim mycdcatalog = new dataset mycdcatalog. readxml (mappath ("cdcatalog. xml") end ifend sub
Then, create a repeater control on the. ASPX page. The content of the
<HTML> <body> <form runat = "server"> <asp: repeater id = "cdcatalog" runat = "server">
then we add a script to create a dataset and bind the mycdcatalog dataset to the Repeater control. We also use HTML tags to fill in the Repeater control and use <% # container. dataitem ("fieldname") %> method to bind a Data project to a cell in the
section:
<% @ Import namespace = "system. data "%> <SCRIPT runat =" server "> sub page_loadif not page. ispostback then dim mycdcatalog = new dataset mycdcatalog. readxml (mappath ("cdcatalog. XML ") cdcatalog. datasource = mycdcatalog cdcatalog. databind () end ifend sub </SCRIPT> <HTML> <body> <form runat = "server"> <asp: repeater id = "cdcatalog" runat = "server"> Use <alternatingitemtemplate>
You can add the <alternatingitemtemplate> element after the <itemtemplate> element to describe the appearance of the alternate row. In the example below, every line in the table will show a light gray background:
<% @ Import namespace = "system. data "%> <SCRIPT runat =" server "> sub page_loadif not page. ispostback then dim mycdcatalog = new dataset mycdcatalog. readxml (mappath ("cdcatalog. XML ") cdcatalog. datasource = mycdcatalog cdcatalog. databind () end ifend sub </SCRIPT> <HTML> <body> <form runat = "server"> <asp: repeater id = "cdcatalog" runat = "server"> <Alternatingitemtemplate>
<Tr bgcolor = "# e8e8e8"> <TD> <% # container. dataitem ("title") %> </TD> <% # container. dataitem ("artist") %> </TD> <% # container. dataitem ("country") %> </TD> <% # container. dataitem ("company") %> </TD> <% # container. dataitem ("price") %> </TD> <% # container. dataitem ("year") %> </TD> </tr> </Alternatingitemtemplate>
<Footertemplate> </table> </footertemplate> </ASP: repeater> </form> </body> Use <separatortemplate>
The <separatortemplate> element can be used to describe the delimiter between each record. The following example inserts a horizontal line between each table row:
<% @ Import namespace = "system. data "%> <SCRIPT runat =" server "> sub page_loadif not page. ispostback then dim mycdcatalog = new dataset mycdcatalog. readxml (mappath ("cdcatalog. XML ") cdcatalog. datasource = mycdcatalog cdcatalog. databind () end ifend sub </SCRIPT> <HTML> <body> <form runat = "server"> <asp: repeater id = "cdcatalog" runat = "server"> <Separatortemplate>
<Tr> <TD colspan = "6"> <HR/> </TD> </tr> </Separatortemplate>
<Footertemplate> </table> </footertemplate> </ASP: repeater> </form> </body>