ควบคุม ASP.NET Repeater
รายการควบคุม Repeater ที่ถูกผูกไว้ที่จะทำซ้ำโปรแกรมควบคุมสำหรับการแสดงผล
ชุดข้อมูลเชื่อมโยงกับการควบคุม Repeater
รายการควบคุม Repeater ที่ถูกผูกไว้ที่จะทำซ้ำโปรแกรมควบคุมสำหรับการแสดงผล การควบคุม Repeater อาจจะผูกพันกับตารางฐานข้อมูลไฟล์ XML หรือรายการอื่น ที่นี่เราจะแสดงให้เห็นถึงวิธีการผูกไฟล์ XML เพื่อควบคุม Repeater
ในตัวอย่างของเราเราจะใช้เอกสารต่อไปนี้ 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
จากนั้นเราก็สร้างตัวควบคุม Repeater ในเพจที่มีขอบ <HeaderTemplate> เนื้อหาองค์ประกอบที่มีการแสดงครั้งแรกและเพียงครั้งเดียวในการส่งออกและ <ItemTemplate> เนื้อหาองค์ประกอบที่สอดคล้องกับชุดข้อมูลแต่ละ "บันทึก" ซ้ำแล้วซ้ำอีกและสุดท้าย <FooterTemplate> เนื้อหาองค์ประกอบ เอาท์พุทปรากฏเพียงครั้งเดียว:
<body>
<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
...
</HeaderTemplate>
<ItemTemplate>
...
</ItemTemplate>
<FooterTemplate>
...
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
จากนั้นเราก็เพิ่มสคริปต์เพื่อสร้างชุดข้อมูลและ binds ชุดข้อมูล mycdcatalog การควบคุม Repeater จากนั้นจะใช้แท็กเพื่อเติมการควบคุม Repeater และ <% # Container.DataItem ( "fieldname")%> การผูกรายการข้อมูลมือถือ <ItemTemplate> พื้นที่:
ตัวอย่าง
<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:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Title</th>
<th>Artist</th>
<th>Country</th>
<th>Company</th>
<th>Price</th>
<th>Year</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
การสาธิต >>
การใช้งาน <AlternatingItemTemplate>
คุณสามารถโพสต์ <ItemTemplate> องค์ประกอบ <AlternatingItemTemplate> องค์ประกอบที่จะใช้ในการอธิบายลักษณะของแถวสลับของการส่งออก ในตัวอย่างด้านล่างตารางจะแสดงทุกสายอื่น ๆ เป็นพื้นหลังสีเทาอ่อน:
ตัวอย่าง
<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:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Title</th>
<th>Artist</th>
<th>Country</th>
<th>Company</th>
<th>Price</th>
<th>Year</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr bgcolor="#e8e8e8">
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
การสาธิต >>
การใช้งาน <SeparatorTemplate>
<SeparatorTemplate> องค์ประกอบที่ใช้เพื่ออธิบายคั่นระหว่างแต่ละระเบียน ในตัวอย่างต่อไปนี้แต่ละตารางจะถูกแทรกระหว่างเส้นแนวนอน:
ตัวอย่าง
<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:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
<table border="0" width="100%">
<tr>
<th>Title</th>
<th>Artist</th>
<th>Country</th>
<th>Company</th>
<th>Price</th>
<th>Year</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Container.DataItem("title")%></td>
<td><%#Container.DataItem("artist")%></td>
<td><%#Container.DataItem("country")%></td>
<td><%#Container.DataItem("company")%></td>
<td><%#Container.DataItem("price")%></td>
<td><%#Container.DataItem("year")%></td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td colspan="6"><hr /></td>
</tr>
</SeparatorTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
การสาธิต >>