Earlier in this chapter, when we bound the contents of a database table to a Repeater control, we used data binding expressions in the Repeater control's ItemTemplate to display the values retrieved from the database. In this section, we examine how you can avoid using data binding expressions in templates.
Why would you want to avoid using data binding expressions? There is nothing wrong with using data binding expressions, and you can always use data binding expressions if you prefer. However, you'll discover that when you need to perform complicated actions inside a template, your code can be more readable when you avoid using data binding expressions.
The trick for avoiding data binding expressions is to handle the ItemDataBound event of the Repeater control. The ItemDataBound event is raised for each item retrieved from the data source. So, if you are binding a Repeater control to a database table that contains 11 authors, then the ItemDataBound event is raised 11 times ”once for each author.
In addition to the ItemDataBound event, the Repeater control also supports the ItemCreated event. The ItemCreated event is raised when each RepeaterItem is created.
The page in Listing 10.17 illustrates how you can handle the ItemDataBound event to display the contents of the Authors database table in a Repeater control.
Listing 10.17 ItemDataBound.aspx
[View full width]
<%@ Import Namespace="System.Data.SqlClient" %> <Script Runat="Server"> Sub Page_Load Dim conPubs As SqlConnection Dim cmdSelect As SqlCommand Dim dtrAuthors As SqlDataReader ' Retrieve records from database conPubs = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=Pubs" ) cmdSelect = New SqlCommand( "Select * From Authors", conPubs ) conPubs.Open() dtrAuthors = cmdSelect.ExecuteReader() ' Bind to Repeater rptAuthors.DataSource = dtrAuthors rptAuthors.DataBind() dtrAuthors.Close() conPubs.Close() End Sub Sub Repeater_ItemDataBound(s As Object, e As RepeaterItemEventArgs) If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then Dim lblAuthor As Label = e.Item.FindControl( "lblAuthor" ) lblAuthor.Text = e.Item.DataItem( "au_lname" ) End If End Sub </Script> <html> <head><title>Repeater.aspx</title></head> <body> <form Runat="Server"> <asp:Repeater ID="rptAuthors" OnItemDataBound="Repeater_ItemDataBound" Runat="Server"> <ItemTemplate> <asp:Label id="lblAuthor" Runat="Server"/> </ItemTemplate> </asp:Repeater> </form> </body> </html>
The C# version of this code can be found on the CD-ROM.
In Listing 10.17, the Repeater_ItemDataBound subroutine is associated with the Repeater control's ItemDataBound event. This subroutine first checks whether it is being called for an ItemTemplate or AlternatingItemTemplate (we want to skip other types of templates such as the HeaderTemplate). Next, the subroutine uses the FindControl() method to retrieve the lblAuthor Label control from the template. Finally, the value of the current DataItem is assigned to the Label .
The Repeater control is not the only control that supports the ItemDataBound event. The DataList and DataGrid controls also support the ItemDataBound event.