Freigeben über


Binden von SQL-Daten an ein DataList-Steuerelement

Im Gegensatz zum Repeater-Steuerelement, einem universellen Iterator, bietet das DataList-Steuerelement zusätzliche, speziell für die Steuerung des Listenlayouts bestimmte Features. Anders als Repeater stellt DataList Tabellenzeilen und -zellen um die durch die Vorlagen definierten Elemente dar, so dass umfassendere Layout- und Formatfunktionen zur Verfügung stehen. DataList unterstützt z. B. die Eigenschaften RepeatColumns und RepeatDirection, die Spaltenanzahl und Richtung (vertikal oder horizontal) für die Darstellung der Datenelemente angeben. Außerdem unterstützt DataList Stilattribute wie Schriftgrad und Schriftartnamen.

Im folgenden Beispiel wird der Zugriff auf eine SQL-Datenbank mit Buchtiteln und anderen Informationen sowie die Anzeige der Daten mit dem DataList-Steuerelement gezeigt. Ein ähnliches Beispiel liefert die Datei DataList2.aspx im Schnelleinstieg zu ASP.NET.

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

Siehe auch

Zugreifen auf Daten mit ASP.NET | Zugreifen auf Daten mit ADO.NET | System.Web.UI.WebControls-Namespace | DataList-Klasse