Binding Data to Controls - The Repeater Control
(Page 2 of 4 )
The Repeater control is used to display records from a database and is declared by using the following ASP.NET tags. The <asp:Repeater> tag requires two attributes. These are ID and Runat, both of these you’ve learned about throughout this book. The ID attribute uniquely identifies the Repeater, and the Runat attribute specifies that this control runs on the server.
Within the Repeater tag is the ItemTemplate. The ItemTemplate specifies what is to be displayed. Here you reference data returned from the database. (You’ll see how to retrieve data later in this section.)
The ItemTemplate tag in turn contains a data binding expression, which is used to reference this data. This expression begins with <%# and ends with %>. The expression itself calls the DataItem() method of the Container object and passes it the column name that identifies the column from the bound data source that the control displays. A Container object is an object that contains other objects.
<asp:Repeater ID="RepeaterID" Runat="Server">
<ItemTemplate>
<%# Container.DataItem("ColumnName") %>
</ItemTemplate>
</asp:Repeater>
Data is retrieved from the database using techniques that you learned in Chapter 10. You’ll recall that you need to open a connection to the database and then pass the database management system (DBMS) a query. A reader is then used to access the result returned by the DBMS.
Data that is retrieved from the DBMS must be bound to the Repeater control. This is accomplished by assigning a reference to the reader to the DataSource property of the Repeater control, and then by calling the Repeater control’s DataBind() method. Every control has a DataBind() function that binds (links) the data source to the control. The control then displays the data once the data source is bound to the control.
Connecting to the database, running the query, and binding the control to the data source typically occurs once in your application. It makes sense to do this when the page is loaded. Therefore, place the code that links to the data in the Page_Load subroutine as shown here. This example connects to the Microsoft SQL Server database, but you can connect to another database, as illustrated in Chapter 10.
This example begins by declaring SqlConnection, SqlCommand, and SqlData-Reader variables. Next, a connection is opened to the DBMS by creating a
SqlConnection object and passing it login information.
An instance of the SqlCommand object is then created, passing it the query that will be sent to the DBMS to retrieve our data. The connection is then opened by calling the Connection object’s Open() method, and data is returned to the reader by calling the SqlCommand object’s ExecuteReader() method.
A reference to the reader is assigned to the DataSource property of the Repeater control, and then the DataBind() method is called to bind the data to the Repeater control. The reader and the connection are then closed.
Sub Page_Load
Dim conCust As SqlConnection
Dim cmdSelectRows As SqlCommand
Dim dtrCust As SqlDataReader
conCust = New SqlConnection( "Server=server;UID=userID;
PWD=password;
Database=database" )
cmdSelectRows = New SqlCommand( "query", conCust)
conCust.Open()
dtrCust = cmdSelectRows.ExecuteReader()
RepeaterControlID.DataSource = dtrCust
RepeaterControlID.DataBind()
dtrCust.Close()
conCust.Close()
End Sub
Let’s assemble these pieces and retrieve data from the Customers table that you created in Chapter 11. The following code shows how this is done. We begin by defining the Page_Load subroutine. The database is called CustomerContactData. Replace MyID and MyPassword with your user ID and password. We’ll use a very simple query that retrieves all the rows and all the columns from the Customers table.
Next, we create a form on our web page that contains the Repeater control. The Repeater control has a data binding expression that calls the DataItem() of the Container object to access the CustomerLastName column of the data returned to the application by the DBMS in response to our query.
<%@ Import Namespace="System.Data.SqlClient" %>
<Script Runat="Server">
Sub Page_Load
Dim conCust As SqlConnection
Dim cmdSelectRows As SqlCommand
Dim dtrCust As SqlDataReader
conCust = New SqlConnection(
"Server=localhost; UID=MyID;PWD=MyPassword; Database=CustomerContactData" )
cmdSelectRows = New SqlCommand( "Select * From custContact", conCust)
conCust.Open()
dtrCust = cmdSelectRows.ExecuteReader()
rptCust.DataSource = dtrCust
rptCust.DataBind()
dtrCust.Close()
conCust.Close()
End Sub
</Script>
<html>
<head><title>Repeater Control Data Binding</title></head>
<body>
<form Runat="Server">
<asp:Repeater ID="rptCust" Runat="Server">
<ItemTemplate>
<%# Container.DataItem("custLastName") %>
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</html>
Next: A Closer Look at Templates >>
More ASP.NET Articles
More By McGraw-Hill/Osborne
|
This article is excerpted from chapter 12 of ASP.NET 2.0 DeMYSTiFieD, written by Jim Keogh (McGraw-Hill/Osborne; ISBN: 0072261412). Check it out today at your favorite bookstore. Buy this book now.
|
|