Latest web development tutorials
×

ASP.NET หลักสูตร

ASP.NET หลักสูตร ASP.NET แนะนำโดยย่อ

WP หลักสูตร

WebPages แนะนำโดยย่อ WebPages Razor WebPages แบบ WebPages โฟลเดอร์ WebPages สถานการณ์โดยรวม WebPages ฟอร์ม WebPages วัตถุ WebPages ไฟล์ WebPages ผู้ช่วย WebPages WebGrid WebPages แผนภูมิ WebPages Email 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 TextBox WebForms Button WebForms ข้อมูลผูกพัน WebForms ArrayList WebForms Hashtable WebForms SortedList WebForms XML ไฟล์ WebForms Repeater WebForms DataList WebForms เชื่อมต่อฐานข้อมูล WebForms หน้าเว็บมาสเตอร์ WebForms การเดินเรือ WebForms ตัวอย่าง

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

WebForms HTML WebForms Controls WebForms Validation

ควบคุม 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 นี้จะทำงานร่วมกับชุดข้อมูลวัตถุ การเรียนการสอนต่อไปนี้จะรวมอยู่ในด้านบนของหน้าขอบนี้:

<%@ 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> เนื้อหาองค์ประกอบที่สอดคล้องกับชุดข้อมูลแต่ละ "บันทึก" ซ้ำแล้วซ้ำอีกและสุดท้าย <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 และ <% # 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>

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

ตัวอย่าง

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

การสาธิต >>