Export (0) Print
Expand All

Binding SQL Data to a DataList Control

.NET Framework 1.1

While the Repeater control is a general-purpose iterator, the DataList control provides additional features specifically for controlling the layout of the list. Unlike Repeater, DataList renders surrounding table rows and cells around its template-defined elements to allow for richer layout and formatting capabilities. For example, DataList supports the RepeatColumns and RepeatDirection properties, which specify the number of columns and the direction (vertical or horizontal) in which to render the data items. DataList also supports style attributes such as font size and font name.

The following example shows how to access a SQL database that contains book titles and other information and display the data using a DataList control. To see a similar example executed, run the DataList2.aspx sample in the ASP.NET QuickStart.

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

See Also

Accessing Data with ASP.NET | Accessing Data with ADO.NET | System.Web.UI.WebControls Namespace | DataList Control

Show:
© 2014 Microsoft