Manage Learn to apply best practices and optimize your operations.

Working with the repeater

Listing showing how to use this .NET control.

.NET Developer Tip
(Receive this column in your inbox,
click Edit your Profile to subscribe.)

Working with the repeater
Jason Lefebvre

ADO.NET, a revolutionary step forward in accessing and manipulating data, and its set of supporting display controls, called list controls, are both incredible. But if you're going to be developing for the .NET world, you'll have to know how to use them. This tip, excerpted from InformIT, discusses one of these controls, the repeater, and offers a listing illustrating its use.

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.

The listing 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, 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.

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. [If] this page does not exist in [y]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: Generating a List of Categories Using the Repeater List Control

<% @Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

  <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;" + _
      Dim cmd as New SqlCommand("Exec Categories_Get ", conn)

      myDataList.DataSource = cmd.ExecuteReader()

    End Sub


<h1>Category Listing</h1>

<form runat="server" id=form1 name=form1>

<asp:repeater id="categories" runat="server">
    <a href='products.aspx?CategoryID=<%# DataBinder.Eval(Container.DataItem,
   "CategoryID") %>'>
      <%# DataBinder.Eval(Container.DataItem, "CategoryName") %>



As mentioned, the lsting generates a list of categories using the Repeater Web control. Lines 11–29 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 [this article], 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.

A Repeater is placed on the Web form in lines 40–47. 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 4–46 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.

To read the article from which this tip comes, click over to InformIT. You have to register there, but the registration is free.

For More Information:

  • Looking for free research? Browse our comprehensive White Papers section by topic, author or keyword.
  • Are you tired of technospeak? The Web Services Advisor column uses plain talk without the hype.
  • For insightful opinion and commentary from today's industry leaders, read our Guest Commentary columns.
  • Hey Codeheads! Start benefiting from other time-saving XML Developer Tips and .NET Developer Tips.
  • Visit our huge Best Web Links for Web Services collection for the freshest editor-selected resources.
  • Choking on the alphabet soup of industry acronyms? Visit our helpful Glossary for the latest lingo.
  • Visit Ask the Experts for answers to your Web services, SOAP, WSDL, XML, .NET, Java and EAI questions.
  • Discuss this issue, voice your opinion or just talk with your peers in the SearchWebServices Discussion Forums.

This was last published in November 2002

Dig Deeper on Microservices pattern, platforms and frameworks



Find more PRO+ content and other member only offers, here.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.