Repeater 控制項是資料繫結清單控制項,外觀全由其樣板控制。Repeater 控制項不同於 DataList,並不會在 HTML 表格中呈現樣板,也沒有內建支援供您選用或編輯。
下列程式碼範例顯示將 Repeater 控制項繫結至 SqlDataReader,後者從 SQL 查詢傳回一組唯讀的順向資料錄,內含一組書籍相關資訊。這個範例使用 SqlDataReader 來達到最佳效能。這個範例中也定義 HeaderTemplate 和 FooterTemplate,分別在清單首尾呈現。
Repeater 控制項重複繫結資料,在 DataSource 集合的每個項目中呈現一次 ItemTemplate。它只呈現其範本中包含的項目。
如需參考類似的範例 (無資料庫存取) 執行,請於 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 類別