Latest web development tutorials

kontrol ASP.NET DataList

DataList kontrol, mirip dengan kontrol Repeater, digunakan untuk mengulang daftar Tampilkan Bindings item pada kontrol ini. Namun, kontrol DataList akan default untuk menambahkan meja untuk item data.


DataSet mengikat kontrol DataList

DataList kontrol, mirip dengan kontrol Repeater, digunakan untuk mengulang daftar Tampilkan Bindings item pada kontrol ini. Namun, kontrol DataList akan default untuk menambahkan meja untuk item data. kontrol DataList dapat terikat tabel database, file XML, atau daftar lain item. Di sini, kita akan menunjukkan bagaimana untuk mengikat sebuah file XML ke kontrol DataList.

Dalam contoh kita, kita akan menggunakan dokumen berikut XML ( "cdcatalog.xml"):

<?xml version="1.0" encoding="ISO-8859-1"?>

<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd>
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country>EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>
</catalog>

Memeriksa file XML: cdcatalog.xml

Pertama, mengimpor "System.Data" namespace. Kita perlu namespace ini untuk bekerja dengan objek DataSet. Instruksi berikut termasuk dalam bagian atas halaman .aspx:

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

Berikutnya, membuat DataSet untuk file XML, dan ketika beban halaman pertama XML loading file ini DataSet:

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycdcatalog=New DataSet
mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
end if
end sub

Kemudian kita membuat kontrol DataList di halaman .aspx. <HeaderTemplate> elemen materi yang diberikan pertama dan hanya sekali dalam output, dan <ItemTemplate> isi elemen yang sesuai dengan DataSet setiap "catatan" diulang, dan akhirnya, <FooterTemplate> isi elemen Output muncul sekali:

<html>
<body>

<form runat="server">
<asp:DataList id="cdcatalog" runat="server">

<HeaderTemplate>
...
</HeaderTemplate>

<ItemTemplate>
...
</ItemTemplate>

<FooterTemplate>
...
</FooterTemplate>

</asp:DataList>
</form>

</body>
</html>

Kemudian kita tambahkan script untuk membuat DataSet, dan mengikat DataSet mycdcatalog ke kontrol DataList. Kemudian mengandung sundulan <HeaderTemplate>, yang berisi data yang akan ditampilkan item <ItemTemplate> dan berisi teks <FooterTemplate> untuk mengisi kontrol DataList. Perhatikan bahwa Anda dapat mengatur DataList Gridlines properti untuk "baik" untuk menunjukkan batas tabel:

contoh

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

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycdcatalog=New DataSet
mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
cdcatalog.DataSource=mycdcatalog
cdcatalog.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">
<asp:DataList id="cdcatalog"
gridlines="both" runat="server">

<HeaderTemplate>
My CD Catalog
</HeaderTemplate>

<ItemTemplate>
"<%#Container.DataItem("title")%>" of
<%#Container.DataItem("artist")%> -
$<%#Container.DataItem("price")%>
</ItemTemplate>

<FooterTemplate>
Copyright Hege Refsnes
</FooterTemplate>

</asp:DataList>
</form>

</body>
</html>

Demonstrasi >>

menggunakan gaya

Anda juga dapat menambahkan gaya ke kontrol DataList, sehingga output lebih mewah:

contoh

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

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycdcatalog=New DataSet
mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
cdcatalog.DataSource=mycdcatalog
cdcatalog.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">
<asp:DataList id="cdcatalog"
runat="server"
cellpadding="2"
cellspacing="2"
borderstyle="inset"
backcolor="#e8e8e8"
width="100%"
headerstyle-font-name="Verdana"
headerstyle-font-size="12pt"
headerstyle-horizontalalign="center"
headerstyle-font-bold="true"
itemstyle-backcolor="#778899"
itemstyle-forecolor="#ffffff"
footerstyle-font-size="9pt"
footerstyle-font-italic="true">

<HeaderTemplate>
My CD Catalog
</HeaderTemplate>

<ItemTemplate>
"<%#Container.DataItem("title")%>" of
<%#Container.DataItem("artist")%> -
$<%#Container.DataItem("price")%>
</ItemTemplate>

<FooterTemplate>
Copyright Hege Refsnes
</FooterTemplate>

</asp:DataList>
</form>

</body>
</html>

Demonstrasi >>

Gunakan <AlternatingItemTemplate>

Anda dapat mengirim <ItemTemplate> elemen <AlternatingItemTemplate> elemen digunakan untuk menggambarkan penampilan bolak baris output. Anda dapat menambahkan gaya untuk data <AlternatingItemTemplate> daerah kontrol DataList di pedalaman:

contoh

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

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycdcatalog=New DataSet
mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
cdcatalog.DataSource=mycdcatalog
cdcatalog.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">
<asp:DataList id="cdcatalog"
runat="server"
cellpadding="2"
cellspacing="2"
borderstyle="inset"
backcolor="#e8e8e8"
width="100%"
headerstyle-font-name="Verdana"
headerstyle-font-size="12pt"
headerstyle-horizontalalign="center"
headerstyle-font-bold="True"
itemstyle-backcolor="#778899"
itemstyle-forecolor="#ffffff"
alternatingitemstyle-backcolor="#e8e8e8"
alternatingitemstyle-forecolor="#000000"
footerstyle-font-size="9pt"
footerstyle-font-italic="True">

<HeaderTemplate>
My CD Catalog
</HeaderTemplate>

<ItemTemplate>
"<%#Container.DataItem("title")%>" of
<%#Container.DataItem("artist")%> -
$<%#Container.DataItem("price")%>
</ItemTemplate>

<AlternatingItemTemplate>
"<%#Container.DataItem("title")%>" of
<%#Container.DataItem("artist")%> -
$<%#Container.DataItem("price")%>
</AlternatingItemTemplate>

<FooterTemplate>
&copy; Hege Refsnes
</FooterTemplate>

</asp:DataList>
</form>

</body>
</html>

Demonstrasi >>