Export (0) Print
Expand All

Binding SQL Data to a Repeater Control

.NET Framework 1.1

The Repeater control is a data-bound list control whose appearance is entirely controlled by its templates. Unlike a DataList, a Repeater control does not render its templates within an HTML table and does not have built-in support for selection or editing.

The following code example shows a Repeater control bound to a SqlDataReader that returns a read-only, forward-only set of data records returned from a SQL query, which contains information about a set of books. A SqlDataReader is used in this example to maximize performance. The example also defines a HeaderTemplate and a FooterTemplate that render at the beginning and end of the list, respectively.

The Repeater control iterates over the bound data, rendering the ItemTemplate once for each item in the DataSource collection. It renders only the elements contained in its templates.

To see a similar example (without database access) executed, run the Repeater1.aspx sample in the ASP.NET QuickStart.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
   <script language="VB" runat="server">
      Sub Page_Load(sender As Object, e As EventArgs) 
         '  Create a connection to the "pubs" SQL database located 
         ' on the local computer.
         Dim myConnection As SqlConnection 
         Dim myCommand As SqlDataAdapter
         ' Connect to the SQL database using a SQL SELECT query to get 
         ' all the data from the "Titles" table.
         myConnection = New SqlConnection("server=localhost;" _ 
            & "database=pubs;Trusted_Connection=Yes")
         myCommand = New SqlDataAdapter("SELECT * FROM Titles", _
            myConnection)
         ' Create and fill a  DataSet.
         Dim ds As Dataset = new DataSet()
         myCommand.Fill(ds)
         ' Bind MyRepeater to the  DataSet. MyRepeater is the ID of the
         ' Repeater control in the HTML section of the page.
         MyRepeater.DataSource = ds
         MyRepeater.DataBind()
      End SUb
   </script>

<body>
   <ASP:Repeater id="MyRepeater" runat="server">
      <HeaderTemplate>
         <Table width="100%" style="font: 8pt verdana">
            <tr style="background-color:DFA894">
               <th>
                  Title
               </th>
               <th>
                  Title ID
               </th>
               <th>
                  Type
               </th>
               <th>
                  Publisher ID
               </th>
               <th>
                  Price
               </th>
            </tr>
      </HeaderTemplate>
      <ItemTemplate>
            <tr style="background-color:FFECD8">
               <td>
                  <%# DataBinder.Eval(Container.DataItem, "title") %>
               </td>
               <td>
                  <%# DataBinder.Eval(Container.DataItem, "title_id") %>
               </td>
               <td>
                  <%# DataBinder.Eval(Container.DataItem, "type") %>
               </td>
               <td>
                  <%# DataBinder.Eval(Container.DataItem, "pub_id") %>
               </td>
               <td>
                  <%# DataBinder.Eval(Container.DataItem, "price", _
                     "{0:c}") %>
               </td>
            </tr>
      </ItemTemplate>
      <FooterTemplate>
         </table>
      </FooterTemplate>
   </ASP:Repeater>
</body>
</html>

[C#]
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
   void Page_Load(Object sender, EventArgs e) 
   {
      //  Create a connection to the "pubs" database located 
      // on the local computer.
      SqlConnection myConnection = new SqlConnection("server=localhost;" +
         "database=pubs;Trusted_Connection=Yes");
      // Connect to the SQL database using a SQL SELECT query to get 
      // all the data from the "Titles" table.
      SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM" +
         " Titles", myConnection);
      // Create and fill a DataSet.
      DataSet ds = new DataSet();
      myCommand.Fill(ds);
      // Bind MyRepeater to the  DataSet. MyRepeater is the ID of the
      // Repeater control in the HTML section of the page.
      MyRepeater.DataSource = ds;
      MyRepeater.DataBind();
   }
</script>

<%--  Display the data in the body of the page. --%>
<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">
   <ASP:Repeater id="MyRepeater" runat="server">
      <HeaderTemplate>
         <Table width="100%" style="font: 8pt verdana">
            <tr style="background-color:DFA894">
               <th>
                  Title
               </th>
               <th>
                  Title ID
               </th>
               <th>
                  Type
               </th>
               <th>
                  Publisher ID
               </th>
               <th>
                  Price
               </th>
            </tr>
      </HeaderTemplate>

      <ItemTemplate>
            <tr style="background-color:FFECD8">
               <td>
                  <%# DataBinder.Eval(Container.DataItem, "title") %>
               </td>
               <td>
                  <%# DataBinder.Eval(Container.DataItem,"title_id") %>
               </td>
               <td>
                  <%# DataBinder.Eval(Container.DataItem, "type") %>
               </td>
               <td>
                  <%# DataBinder.Eval(Container.DataItem, "pub_id") %>
               </td>
               <td>
                  <%# DataBinder.Eval(Container.DataItem, 
                     "price", "{0:c}") %>
               </td>
            </tr>
      </ItemTemplate>

      <FooterTemplate>
         </Table>
      </FooterTemplate>
   </ASP:Repeater>
</body>
</html>

See Also

Accessing Data with ASP.NET | Accessing Data with ADO.NET | System.Web.UI.WebControls Namespace | Repeater Class

Show:
© 2014 Microsoft