Repeater コントロールは汎用の反復子です。これに対して、DataList コントロールは、特にリストのレイアウトを制御するための追加機能を提供します。Repeater とは異なり、DataList は、テンプレートで定義された要素の周囲を取り囲むテーブル行およびセルを表示して、より豊富なレイアウトおよび書式機能を提供します。たとえば、DataList は、データ項目を表示する列の数および方向 (縦または横) を指定する RepeatColumns プロパティおよび RepeatDirection プロパティをサポートします。DataList は、フォント サイズやフォント名などのスタイル属性もサポートします。
DataList コントロールを使用して、ブック タイトルおよびその他の情報を含む SQL データベースにアクセスし、データを表示する方法の例を次に示します。同様の例の実行結果を見るには、「ASP.NET クイック スタート」に示す DataList2.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)
Dim myConnection As SqlConnection
Dim myCommand As SqlDataAdapter
' Create a connection to the "pubs" SQL database located on the
' local computer.
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.
myCommand = New SqlDataAdapter("SELECT * FROM Titles", myConnection)
' Create and fill a DataSet.
Dim ds As DataSet = new DataSet()
myCommand.Fill(ds)
' Bind MyDataList to the DataSet. MyDataList is the ID for
' the DataList control in the HTML section of the page.
MyDataList.DataSource = ds
MyDataList.DataBind()
End Sub
</script>
<%-- Display the data. -%>
<body>
<%-- Open the DataList control and set it for two columns, to be
filled in horizontal order. --%>
<ASP:DataList id="MyDataList" RepeatColumns="2"
RepeatDirection="Horizontal" runat="server">
<ItemTemplate>
<div style="padding:15,15,15,15;font-size:10pt;font-family:Verdana">
<div style="font:12pt verdana;color:darkred">
<i><b><%# DataBinder.Eval(Container.DataItem, "title")%>
</i></b>
</div>
<br>
<b>Title ID: </b>
<%# DataBinder.Eval(Container.DataItem, "title_id") %><br>
<b>Category: </b>
<%# DataBinder.Eval(Container.DataItem, "type")%><br>
<b>Publisher ID: </b>
<%# DataBinder.Eval(Container.DataItem, "pub_id") %><br>
<b>Price: </b>
<%# DataBinder.Eval(Container.DataItem, "price", "{0:c}") %>
<p>
</div>
</ItemTemplate>
</ASP:DataList>
</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" SQL 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 MyDataList to the DataSet. MyDataList is the ID for
// the DataList control in the HTML section of the page.
MyDataList.DataSource = ds;
MyDataList.DataBind();
}
</script>
<%-- Display the data. -%>
<body>
<%-- Open the DataList control and set it for two columns, to be
filled in horizontal order. --%>
<ASP:DataList id="MyDataList" RepeatColumns="2"
RepeatDirection= "Horizontal" runat="server">
<%-- Create a DataList control template named "ItemTemplate". --%>
<ItemTemplate>
<div style="padding:15,15,15,15;font-size:10pt;
font-family:Verdana">
<div style="font:12pt verdana;color:darkred">
<i><b><%# DataBinder.Eval(Container.DataItem, "title")%>
</i></b>
</div>
<br>
<b>Title ID: </b>
<%# DataBinder.Eval(Container.DataItem, "title_id") %><br>
<b>Category: </b>
<%# DataBinder.Eval(Container.DataItem, "type")%><br>
<b>Publisher ID: </b>
<%# DataBinder.Eval(Container.DataItem, "pub_id") %><br>
<b>Price: </b>
<%# DataBinder.Eval(Container.DataItem,"price", "{0:c}") %>
<p>
</div>
</ItemTemplate>
</ASP:DataList>
</body>
</html>
参照
ASP.NET でのデータ アクセス | ADO.NET を使用したデータのアクセス | System.Web.UI.WebControls | DataList クラス