<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
<title>Binding a DataReader to a Repeater Control</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server">
protected void Page_Load ( object src, EventArgs e ) {
// specify the data source
OleDbConnection myConn = new OleDbConnection (
"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" +
Server.MapPath ( "~/app_data/dbtutor.mdb" ) );
// define the command query
OleDbCommand myCmd = new OleDbCommand ( "SELECT * FROM Products", myConn );
// open the connection
myConn.Open ( );
// bind the repeater to the reader
myRepeater.DataSource = myCmd.ExecuteReader (
CommandBehavior.CloseConnection );
myRepeater.DataBind ( );
// close the connection
myConn.Close ( );
}
</script>
<body>
<!-- #include virtual="~/shared/top.inc -->
<div class="header"><h2>Binding a <span class="hilite">DataReader</span> to a <span class="hilite">Repeater</span> Control</h2></div>
<!-- #include virtual="~/shared/viewsrc_top.inc" -->
<hr size=1 width=92%>
<p align="center">
<form runat="server">
<asp:repeater id="myRepeater" runat="server">
<headertemplate>
<table width="92%" cellspacing=1 cellpadding=5 style="font:10pt verdana">
<tr style="background:brown; color:snow; height:25pt">
<th>ProductType</th>
<th>ProductCode</th>
<th>ProductName</th>
<th>ProductDescription</th>
<th>UnitPrice</th></tr>
</headertemplate>
<itemtemplate>
<tr>
<td><%# Eval ( "ProductType" ) %></td>
<td><a href='details_prod.aspx?id=<%# Eval ( "ProductCode" ) %>'>
<%# Eval ( "ProductCode" ) %></a></td>
<td><%# Eval ( "ProductName" ) %></td>
<td><%# Eval ( "ProductDescription" ) %></td>
<td align="right"><%# Eval ( "UnitPrice", "{0:c}" ) %></td></tr>
</itemtemplate>
<alternatingitemtemplate>
<tr style="background-color:beige">
<td><%# Eval ( "ProductType" ) %></td>
<td><a href='details_prod.aspx?id=<%# Eval ( "ProductCode" ) %>'>
<%# Eval ( "ProductCode" ) %></a></td>
<td><%# Eval ( "ProductName" ) %></td>
<td><%# Eval ( "ProductDescription" ) %></td>
<td align="right"><%# Eval ( "UnitPrice", "{0:c}" ) %></td></tr>
</alternatingitemtemplate>
<footertemplate>
</table>
</footertemplate>
</asp:repeater>
</form>
</p>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>