ASP.NET - Repeater ควบคุม

Repeater ควบคุมใช้เพื่อแสดงรายการที่ถูกสำรวจอยู่ในรายการที่ถูกจำกัดโดยควบคุมนี้

การจับต่อ DataSet ไปยัง Repeater ควบคุม

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

我们将在例子中使用下面的 XML 文件("cdcatalog.xml"):




  Empire Burlesque
  Bob Dylan
  USA
  Columbia
  10.90
  1985


  Hide your heart
  Bonnie Tyler
  UK
  CBS Records
  9.90
  1988


  Greatest Hits
  Dolly Parton
  USA
  RCA
  9.90
  1982


  Still got the blues
  Gary Moore
  UK
  Virgin records
  10.20
  1990


  Eros
  Eros Ramazzotti
  EU
  BMG
  9.90
  1997


请查看该 XML 文件:cdcatalog.xml

首先,导入 "System.Data" 命名空间。我们需要此命名空间与 DataSet 对象一同工作。在 .aspx 页面的顶部包含下面这条指令:

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

ต่อมา สร้างเครื่องมือ Repeater ในหน้า .aspx ของเรา <HeaderTemplate> ที่มีในการออกที่แสดงครั้งเดียว และ <ItemTemplate> ที่มีในการออกที่ตอบคำนึงตาม "record" ใน DataSet ที่มีการละตะละและในที่สุด <FooterTemplate> ที่มีในการออกที่แสดงครั้งเดียว

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

หลังจากนั้นเราสร้าง DataSet สำหรับ XML แบบนี้และนำ XML ภายในไฟล์เข้ามาเก็บใน DataSet ขณะที่เว็บเพจที่มีการโหลดเปิดครั้งแรก

<html>
<body>
<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
...
</HeaderTemplate>
<ItemTemplate>
...
</ItemTemplate>
<FooterTemplate>
...
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>

หลังจากนั้นเราเพิ่มสคริปต์ที่สามารถสร้าง DataSet และจอง DataSet ของ mycdcatalog ที่มีการจับคู่กับเครื่องมือ Repeater นี้ เราใช้สกัดแทนที่ HTML ที่เติมเครื่องมือ Repeater และจับคู่ข้อมูลโดยใช้ข้อความ <%#Container.DataItem("fieldname")%> ในช่องที่อยู่ใน <ItemTemplate>

<%@ 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: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>

คุณสามารถเพิ่ม Element <AlternatingItemTemplate> หลังจาก Element <ItemTemplate> ตามที่เรียกร้อง เพื่อบรรยายลักษณะแถวที่แลกเปลี่ยน ในตัวอย่างดังนี้ แถวของตารางจะแสดงด้วยพื้นหลังสีขาวและขาวน้ำเงินในแถวที่แลกเปลี่ยน

<%@ 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: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>

Element <SeparatorTemplate> สามารถใช้เพื่อบรรยายสัญญาณแบ่งรายการระหว่างบันทึกต่าง ๆ ตัวอย่างดังนี้ ในแถวของตารางในแบบภาพแบ่งแยกแถวจะมีเส้นตรงน้ำเงินลาย

<%@ 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: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>

แสดงตัวอย่างนี้