次の方法で共有


Repeater コントロールへの SQL データの連結

Repeater コントロールは、外観全体がそのテンプレートによって制御されるデータ連結リスト コントロールです。DataList とは異なり、Repeater コントロールは、HTML テーブル内にテンプレートを表示しません。また、選択や編集のためのサポートも組み込まれていません。

SQL クエリから返された読み取り専用、前方参照専用の一連のデータ レコードを返す SqlDataReader に連結された Repeater コントロールを次のコード例に示します。この例では、最大のパフォーマンスを得るために SqlDataReader が使用されています。また、この例では、それぞれ、リストの先頭と末尾を表示する HeaderTemplate および FooterTemplate が定義されています。

Repeater コントロールは、DataSource コレクションの項目ごとに連結データを反復し、ItemTemplate を 1 回レンダリングします。Repeater コントロールのテンプレートに含まれている要素だけが表示されます。

同様の例 (データベース アクセスを使用しない例) の実行結果を見るには、「ASP.NET クイック スタート」に示す Repeater1.aspx サンプルを実行してください。

<%@ 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>

参照

ASP.NET でのデータ アクセス | ADO.NET を使用したデータのアクセス | System.Web.UI.WebControls | Repeater クラス