共用方式為


將 SQL 資料繫結至 DataList 控制項

Repeater 控制項是一般用途的 iterator,相較下 DataList 控制項則提供專門控制清單配置的其他功能。與 Repeater 不同的是,DataList 會在其樣板定義的項目周圍呈現繞邊的資料表資料列和儲存格,提供您更豐富的配置和格式化空間。例如,DataList 支援 RepeatColumnsRepeatDirection 屬性,可指定呈現資料項目的資料行數目和方向 (垂直或水平)。DataList 也支援字型大小和字型名稱等樣式屬性。

下列範例顯示如何存取包含書籍標題等其他資料的 SQL 料庫,並使用 DataList 控制項顯示資料。如需參考類似的範例執行,請於 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 控制項