Enlazar datos SQL a un control DataList
Mientras que el control Repeater es un iterador de propósito general, el control DataList ofrece funciones adicionales para controlar específicamente el diseño de la lista. A diferencia de Repeater, DataList procesa las filas y celdas de tabla que hay alrededor de los elementos definidos por su plantilla con el fin de permitir que haya mayores posibilidades de diseño y formato. Por ejemplo, DataList admite las propiedades RepeatColumns y RepeatDirection, que especifican el número de columnas y la dirección (vertical u horizontal) donde procesar los elementos de datos. DataList también admite atributos de estilo como el tamaño y el nombre de la fuente.
En el ejemplo siguiente se muestra cómo tener acceso a una base de datos SQL que contiene títulos de libros y otra información, y cómo mostrar los datos mediante un control DataList. Para ver la ejecución de un ejemplo similar, ejecute el ejemplo DataList2.aspx del Tutorial de 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>
Vea también
Acceso a datos con ASP.NET | Acceso a datos con ADO.NET | System.Web.UI.WebControls (Espacio de nombres) | DataList (Control)