80 likes | 160 Vues
CIS 375—Web App Dev II. ASP .NET 8 More Binding. The Repeater Control 1. 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.
E N D
CIS 375—Web App Dev II ASP .NET 8 More Binding
The Repeater Control 1 • 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. • We will use cdcatalog.xml in our examples. <?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>
The Repeater Control 2 • The code starts out by importing the ____________, using a DataSet object, associating it with the XML file, and binding when the page first loads: <%@ 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>
The Repeater Control 3 • 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 “________" in the DataSet • Last, the contents of the <FooterTemplate> element are rendered once within the output.
The Repeater Control 4--Example <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> </html> </body>
Alternating and Separating • You can add an <AlternatingItemTemplate> element following the <ItemTemplate> element to describe the ___________ of alternating rows. (Ex.) <AlternatingItemTemplate> <tr bgcolor="#e8e8e8"> <td><%#Container.DataItem("title")%></td> … <td><%#Container.DataItem("year")%></td> </tr> </AlternatingItemTemplate> • The <SeparatorTemplate> element can be used to describe a separator between each record. (Ex.) <SeparatorTemplate> <tr><td colspan="6"><hr /></td> </tr> </SeparatorTemplate>
The DataList Control • The DataList control, like the Repeater control, is used to display a repeated list of items that are bound to the control. • However, the DataList control adds a _______ around the data items by default. • Instead of • <form runat="server"> • <asp:Repeater id="cdcatalog" runat="server"> • We have • <form runat="server"> • <asp:Datalist id="cdcatalog" runat="server"> • DataList Example
Using Styles • Creating styles for a DataList: <asp:DataList id="cdcatalog" runat="server" cellpadding="2" backcolor="#e8e8e8" width="100%" headerstyle-font-name="Verdana" headerstyle-font-size="12pt" headerstyle-horizontalalign="center" itemstyle-backcolor="#778899" alternatingitemstyle-backcolor="#e8e8e8" footerstyle-font-size="9pt" footerstyle-font-italic="true"> • Styles Example • AlternatingItem Style Example