Controls You Can Use on Web Forms ASP.NET Data Controls Repeater Control
Essentially, to create a working Repeater control, you need to add the control to the page and bind the control to a data source.
The below procedures show the minimum steps needed to display data in a Repeater control.
This first example shows a Repeater control that is bound to a SqlDataSource control.
- Declare an <
asp:repeater
> element on the page. For syntax, see Repeater Control Syntax.
- Specify the control’s DataSourceID property.
- Optionally set the control’s base properties.
<asp:repeater id="myRepeater"
datasourceid="customers"
width="90%"
runat="server" >
- Within the Repeater declaration, declare an <ItemTemplate> element.
- Within the ItemTemplate, define each field from the data source that you intend to present.
To display the value of a field in the ItemTemplate, use DataBinding
Expression Syntax.
<itemtemplate>
<b>Product Code:</b> <%# Eval ( "ProductCode" ) %>
<br>
...
<b>Price:</b> <%# Eval ( "UnitPrice", "{0:c}" ) %>
</itemtemplate>
NOTE: The ItemTemplate must contain at least one databinding expression that binds a control to a field from the data source; otherwise the Repeater has nothing to render.
The DataSource property of a Repeater can also be set programmatically, just like any Web server control property. Below shows an example of binding a data source to a Repeater in code.
<script language="C#" runat="server">
void Page_Load ( object src, EventArgs e ) {
string query = "SELECT * FROM pubs_Titles";
myRepeater.DataSource = fetchReader ( query, "pubs" );
myRepeater.DataBind ( );
}
</script>
The above examples apply several concepts and methods that are described elsewhere in this workshop. For particulars, see ADO.NET Primer, Introduction to Data Binding in Web Forms, Web Server Controls Templates.
Introduction to the Repeater Control Responding to Events in Repeater Items