ล่าสุดการพัฒนาเว็บบทเรียน
×

ASP.NET เกี่ยวกับการสอน

ASP.NET บ้าน ASP.NET แนะนำ

WP เกี่ยวกับการสอน

WebPages แนะนำ WebPages มีดโกน WebPages แบบ WebPages โฟลเดอร์ WebPages สถานการณ์โดยรวม WebPages ฟอร์ม WebPages วัตถุ WebPages ไฟล์ WebPages ฐานข้อมูล WebPages ผู้ช่วย WebPages WebGrid WebPages ชาร์ต WebPages อีเมล์ WebPages PHP WebPages ประกาศ WebPages ตัวอย่าง

WP คู่มืออ้างอิง

WebPages ชั้นเรียน WebPages ความปลอดภัย WebPages ฐานข้อมูล WebPages เว็บเมล์ WebPages ผู้ช่วย

ASP.NET Razor

Razor แนะนำ Razor วากยสัมพันธ์ Razor C# ตัวแปร Razor C# ลูป Razor C# ตรรกะ Razor VB ตัวแปร Razor VB ลูป Razor VB ตรรกะ

ASP.NET MVC

MVC แนะนำ MVC ใบสมัคร MVC โฟลเดอร์ MVC แบบ MVC ตัวควบคุม MVC เข้าชม MVC ฐานข้อมูล MVC แบบ MVC ความปลอดภัย MVC HTML ผู้ช่วย MVC ประกาศ MVC การอ้างอิง

WF สอน

WebForms แนะนำ WebForms หน้า WebForms การควบคุม WebForms เหตุการณ์ WebForms ฟอร์ม WebForms ViewState WebForms กล่องข้อความ WebForms ปุ่ม WebForms ข้อมูลผูกพัน WebForms ArrayList WebForms Hashtable WebForms SortedList WebForms XML ไฟล์ WebForms Repeater WebForms DataList WebForms DbConnection WebForms Master หน้า WebForms การเดินเรือ WebForms ตัวอย่าง


 

ASP.NETรูปแบบเว็บ - ควบคุม DataList


การควบคุม DataList เป็นเช่นควบคุม Repeater ที่ใช้ในการแสดงรายการซ้ำของรายการที่จะผูกพันที่จะควบคุม อย่างไรก็ตามการควบคุม DataList เพิ่มตารางรอบรายการข้อมูลโดยค่าเริ่มต้น


ผูกชุดข้อมูลเป็นตัวควบคุม DataList

การควบคุม DataList เป็นเช่นควบคุม Repeater ที่ใช้ในการแสดงรายการซ้ำของรายการที่จะผูกพันที่จะควบคุม อย่างไรก็ตามการควบคุม DataList เพิ่มตารางรอบรายการข้อมูลโดยค่าเริ่มต้น การควบคุม DataList อาจจะผูกพันกับตารางฐานข้อมูลไฟล์ XML หรือรายการอื่น ที่นี่เราจะแสดงวิธีการผูกไฟล์ XML เพื่อควบคุม DataList

เราจะใช้แฟ้มต่อไปนี้ 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>

ลองดูที่ไฟล์ XML นี้: cdcatalog.xml

ครั้งแรกที่นำเข้า "System.Data" namespace เราจำเป็นต้อง namespace นี้จะทำงานร่วมกับชุดข้อมูลวัตถุ รวมถึงคำสั่งดังต่อไปนี้ที่ด้านบนของหน้า .aspx นี้:

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

ถัดไปสร้างชุดข้อมูลสำหรับไฟล์ XML และโหลดไฟล์ XML ไปยังชุดข้อมูลเมื่อเพจที่มีการโหลดครั้งแรก:

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

จากนั้นเราจะสร้าง DataList ในหน้าขอบ เนื้อหาของ <HeaderTemplate> องค์ประกอบที่จะกลายเป็นครั้งแรกและเพียงครั้งเดียวภายในการส่งออกแล้วเนื้อหาของ <ItemTemplate> องค์ประกอบที่มีการทำซ้ำสำหรับแต่ละ "record" ในชุดและที่ผ่านมาเนื้อหาของ <FooterTemplate> องค์ประกอบที่มี การแสดงผลครั้งภายในการส่งออก:

<html>
<body>

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

<HeaderTemplate>
...
</HeaderTemplate>

<ItemTemplate>
...
</ItemTemplate>

<FooterTemplate>
...
</FooterTemplate>

</asp:DataList>
</form>

</body>
</html>

จากนั้นเราก็เพิ่มสคริปต์ที่สร้างชุดข้อมูลและ binds ชุดข้อมูล mycdcatalog เพื่อควบคุม DataList นอกจากนี้เรายังเติมควบคุม DataList กับ <HeaderTemplate> ที่มีส่วนหัวของตารางที่ <ItemTemplate> ที่มีรายการข้อมูลที่จะแสดงและ <FooterTemplate> ที่มีข้อความ โปรดทราบว่า gridlines แอตทริบิวต์ของ DataList มีการตั้งค่า "both" เพื่อแสดงเส้นขอบตาราง:

ตัวอย่าง

<%@ 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>
แสดงตัวอย่าง»

โดยใช้รูปแบบ

นอกจากนี้คุณยังสามารถเพิ่มรูปแบบการควบคุม DataList ที่จะทำให้การส่งออกแฟนซีเพิ่มเติมได้ที่:

ตัวอย่าง

<%@ 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>
แสดงตัวอย่าง»

ใช้ <AlternatingItemTemplate>

คุณสามารถเพิ่ม <AlternatingItemTemplate> องค์ประกอบหลังจาก <ItemTemplate> องค์ประกอบในการอธิบายลักษณะของแถวสลับของการส่งออก คุณอาจจะจัดรูปแบบข้อมูลในส่วน <AlternatingItemTemplate> ส่วนในการควบคุม DataList:

ตัวอย่าง

<%@ 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>
แสดงตัวอย่าง»