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

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รูปแบบเว็บ - ควบคุม Repeater


การควบคุม Repeater จะใช้ในการแสดงรายการซ้ำของรายการที่จะผูกพันที่จะควบคุม


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

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

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

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

<html>
<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 กับแท็ก HTML และผูกรายการข้อมูลไปยังเซลล์ใน <ItemTemplate> ส่วนกับ <% # คอนเทนเนอร์ DataItem("fieldname") %> วิธีการ:

ตัวอย่าง

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

คุณสามารถเพิ่ม <AlternatingItemTemplate> องค์ประกอบหลังจาก <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>

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