ควบคุม ASP.NET DataList
ควบคุม DataList คล้ายกับการควบคุม Repeater ที่ใช้ในการทำซ้ำรายการรายการที่แสดงการรวมในการควบคุมนี้ อย่างไรก็ตามการควบคุม DataList จะเริ่มต้นที่จะเพิ่มตารางไปยังรายการข้อมูล
ชุดข้อมูลเชื่อมโยงกับการควบคุม DataList
ควบคุม DataList คล้ายกับการควบคุม Repeater ที่ใช้ในการทำซ้ำรายการรายการที่แสดงการรวมในการควบคุมนี้ อย่างไรก็ตามการควบคุม DataList จะเริ่มต้นที่จะเพิ่มตารางไปยังรายการข้อมูล ควบคุม DataList อาจจะผูกพันกับตารางฐานข้อมูลไฟล์ XML หรือรายการอื่น ที่นี่เราจะแสดงให้เห็นถึงวิธีการผูกไฟล์ XML เพื่อควบคุม DataList
ในตัวอย่างของเราเราจะใช้เอกสารต่อไปนี้ XML ( "cdcatalog.xml"):
<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>
ตรวจสอบไฟล์ XML: cdcatalog.xml
ครั้งแรกที่นำเข้า "System.Data" namespace เราจำเป็นต้อง namespace นี้จะทำงานร่วมกับชุดข้อมูลวัตถุ การเรียนการสอนต่อไปนี้จะรวมอยู่ในด้านบนของหน้าขอบนี้:
ถัดไปสร้างชุดข้อมูลสำหรับไฟล์ XML และเมื่อโหลดแรกหน้านี้ XML โหลดไฟล์ชุดข้อมูล:
sub Page_Load
if Not Page.IsPostBack then
dim mycdcatalog=New DataSet
mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
end if
end sub
จากนั้นเราก็สร้างตัวควบคุม DataList ในเพจที่มีขอบ <HeaderTemplate> เนื้อหาองค์ประกอบที่มีการแสดงครั้งแรกและเพียงครั้งเดียวในการส่งออกและ <ItemTemplate> เนื้อหาองค์ประกอบที่สอดคล้องกับชุดข้อมูลแต่ละ "บันทึก" ซ้ำแล้วซ้ำอีกและสุดท้าย <FooterTemplate> เนื้อหาองค์ประกอบ เอาท์พุทปรากฏเพียงครั้งเดียว:
<body>
<form runat="server">
<asp:DataList id="cdcatalog" runat="server">
<HeaderTemplate>
...
</HeaderTemplate>
<ItemTemplate>
...
</ItemTemplate>
<FooterTemplate>
...
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>
จากนั้นเราก็เพิ่มสคริปต์เพื่อสร้างชุดข้อมูลและ binds ชุดข้อมูล mycdcatalog การควบคุม DataList รายการแล้วส่วนหัวที่มี <HeaderTemplate> มีข้อมูลที่จะแสดง <ItemTemplate> และมีข้อความ <FooterTemplate> เพื่อเติมควบคุม DataList โปรดทราบว่าคุณสามารถตั้งค่า DataList เส้นตารางคุณสมบัติการ "ทั้ง" เพื่อแสดงเส้นขอบตาราง:
ตัวอย่าง
<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>
การสาธิต >>
โดยใช้รูปแบบ
นอกจากนี้คุณยังสามารถเพิ่มสไตล์การควบคุม DataList เพื่อการส่งออกเป็นแฟนซีเพิ่มเติมได้ที่:
ตัวอย่าง
<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>
การสาธิต >>
การใช้งาน <AlternatingItemTemplate>
คุณสามารถโพสต์ <ItemTemplate> องค์ประกอบ <AlternatingItemTemplate> องค์ประกอบที่จะใช้ในการอธิบายลักษณะของแถวสลับของการส่งออก คุณสามารถเพิ่มรูปแบบให้ข้อมูล <AlternatingItemTemplate> เขตควบคุม DataList ในการตกแต่งภายใน:
ตัวอย่าง
<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>
© Hege Refsnes
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>
การสาธิต >>