Working with the Repeater
The Repeater is the most basic of the built-in list control objects, yet offers a great deal of flexibility. To format the data it displays, the Repeater uses templates. Templates are blocks of HTML mixed with server logic that define how each row in the data collection will appear when sent to the client. Templates are defined within the body of the control instantiated within the Web form.
There are a number of different types of templates. The ItemTemplate, as seen in Listing 11.1, is responsible for formatting each record in the collection. The AlternatingItemTemplate lets you define a special appearance for every other item in the collection. A typical use of this would be to define a slightly different color for alternating items, to make the records displayed easier to read. The Repeater also supports templates that enable you to define a header and footer as well as a separator that is inserted between each item displayed.
Listing 11.1 shows how to use the Repeater list control to display a list of categories from the database. The server-side code should look familiar. ADO.NET calls a stored procedure named Categories_Get (seen in Listing 11.2), which returns a resultset containing the CategoryID and CategoryName for each item in the Categories table of the Northwind database. During data binding, the Repeater uses the ItemTemplate to format the data being returned from the database. In this case, the data returned is used to create a set of hyperlinks for the categories. Figure 11.1 shows how the example in Listing 11.1 appears when viewed in a Web browser.
Inside the ItemTemplate, notice that to insert fields from the resultset, you use the DataBinder.Eval() method. The template will insert the value of the field from the data source where this tag is placed.
The category links generated in Listing 11.1 reference a page named Products.aspx. Because this page does not exist in our application, the link will generate an error. However, it's rather easy to create a page that accepts the selected category ID and returns a list of products for that category.
Listing 11.1 Generating a List of Categories Using the Repeater List Control
<% @Page Language="VB" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <HTML> <HEAD> <LINK rel="stylesheet" type="text/css" href="Main.css"> <!-- End Style Sheet --> <script language="VB" runat="server" > Sub Page_Load(Source as Object, E as EventArgs) LoadGridData( categories ) End Sub Private Sub LoadGridData( _ myDataList as System.Web.UI.WebControls.Repeater ) Dim conn as New SqlConnection("Initial Catalog=Northwind;" + _ "Server=(local);UID=sa;PWD=;") Dim cmd as New SqlCommand("Exec Categories_Get ", conn) conn.Open() myDataList.DataSource = cmd.ExecuteReader() myDataList.DataBind() conn.Close() End Sub </script> </HEAD> <BODY> <h1>Category Listing</h1> <hr> <form runat="server" id=form1 name=form1> <asp:repeater id="categories" runat="server"> <ItemTemplate> <a href='products.aspx?CategoryID=<%# DataBinder.Eval(Container.DataItem, "CategoryID") %>'> <%# DataBinder.Eval(Container.DataItem, "CategoryName") %> </a><br> </ItemTemplate> </asp:repeater> </form> <hr> </BODY> </HTML>
As mentioned, Listing 11.1 generates a list of categories using the Repeater Web control. Lines 1129 query the database, retreive a resultset, and then bind the data to the Repeater. In this example, the actual loading and binding of data has been moved into a separate method named LoadGridData(). This convention will be used more often for the remainder of the hours in this book, because many examples retrieve data from multiple sources, and this helps to make the code more readable. As you'll notice in line 24, we're using a stored procedure to generate the resultset. This resultset is no different than one you'd get by manually building the SELECT SQL query manually. For more information on using stored procedures, see Hour 15, "Working With Stored Procedures."
A Repeater is placed on the Web form in lines 4047. Until now, you've only seen data displayed using a DataGrid or a label Web control. The Repeater is similar to the DataGrid, in that it is used to display and format data returned from the data source. Lines 446 define the ItemTemplate for the Repeater. The ItemTemplate contains the HTML that is generated for each row in the resultset returned from the data source. In this case, it builds a hyperlink for each category. The DataBinder.Eval() method is used to insert the value of the field from the resultset into the HTML output.
Listing 11.2 The Stored Procedure Used to Return Data for the Example in Listing 11.1
CREATE PROCEDURE Categories_Get AS SELECT CategoryID, CategoryName FROM Categories
Listing 11.2 contains the SQL query you can use to create the Categories_Get stored procedure used in Listing 11.1. You can think of this stored procedure as a method that exists in the database that is optimized for returning the data in this query. You can add the stored procedure in Listing 11.2 to your Northwind database by placing Listing 11.2 into the Query Analyzer and running the query against the Northwind database.
Figure 11.1 The appearance of the repeater control from Listing 11.1.
The Repeater is a generic list control best suited for generic uses. However, the DataGrid and the DataList are a bit more specialized, as you'll see in the next few sections.