Ràng buộc dữ liệu với Repeater Control

Một phần của tài liệu Tổng quát về .NET Framework và ASP.NET (Trang 53 - 54)

Repeater control là control ràng buộc dữ liệu kiểu danh sách. Không giống nh DataList, Reapeter không trả về các kiểu mẫu của nó với các bảng HTML không đợc xây dựng để hỗ trợ cho việc lựa chọn và sửa đổị

Ràng buộc dữ liệu với DataList Control

Trong khi Repeater control đợc thiết kế với mục đích lặp lại, DataList control cung cấp thêm các chức năng để dễ dàng điều khiển cách trình bày của danh sách. DataList Control trả về các hàng và ô trong bảng những định dạng của nó, cho phép tạo ra nhiều khả năng trình bày và định dạng phong phú. DataList Control cung cấp hai thuộc tính là RepeatColumns và RepeatDirection để xác định giá trị cụ thể cho cột và hớng trong khi nó trả về dữ liệụ Ví dụ:

Visual Basic]

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Datạ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

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 pagẹ MyDataList.DataSource = ds

MyDataList.DataBind() End Sub

</script>

<%-- Display the datạ -%> <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="pađing: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>

Một phần của tài liệu Tổng quát về .NET Framework và ASP.NET (Trang 53 - 54)