เว็บโฮสติ้งเอื้ออาทร
Welcome :Guest  ( Register - Login )
  Active TopicsActive Topics  Display List of Forum Membersรายชื่อสมาชิก  Search The ForumSearch
Menu
  Home
  ASP
  ASP.NET
  SQL
  PHP
  Forum(s)
  Guest Book
    Sign Guestbook
  Download
  Contact us
  ติดต่อโฆษณา
Member Online
Total Users Online: 1

We have
  0 Member(s)
  1 Guest(s)
  0 Anonymous.

Most User Online: 8042
Occured: 06 April 2008

Members Online:



[ View Full List ]
[Based on the last 10 minutes]

You are visitor number 3118743
Repeater
Repeater

      เป็นคอนโทรลที่ใช้ในการแสดงข้อมูลแบบธรรมดา คือ เราเป็นผู้กำหนดได้เองว่าจะให้แสดงออกมาในรูปแบบใดหรือคิดอีกแง่หนึ่งคอนโทรลตัวนี้แทนคำสั่งการวนลูปประเภท For หรือ While นั่นเอง

<asp:Repeater id = "name1" runat = "server"/>

      จากตัวอย่างเป็นการใช้งานคอนโทรล Repeater โดยมีพร็อพเพอร์ตี้ id ใช้การกำหนดชื่อให้กับคอนโทรลนี้ เพื่อใช้อ้างถึงคอนโทรลนี้ในการเขียนโปรแกรม พร็อพเพอร์ตี้นี้จึงจำเป็นต้องมีทุกครั้งที่มีการเรียกใช้คอนโทรลตัวนี้

ในตัวอย่างนี้ผมขอแสดงการใช้ Repeater เพื่อแสดงข้อมูลในไฟล์ XML ออกมาซึ่งไฟล์ XML นี้ชื่อว่า testrepeater.xml ซึ่งมีข้อมุลดังนี้

<?xml version="1.0" encoding="UTF-8"?>
<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>
คุณสามารถดูเนื้อหาของไฟล์นี้ได้ที่ testrepeater.xml

เริ่มต้นสำหรับการใช้คำสั่ง Repeater คือการเรียก Namespace ที่เป็น DataSet Object ของ ASP.NET ที่ใช้ในการติดต่อหรือดึงข้อมูลภานนอกเข้ามาซึ่งคุณสามารถดูรายละเอียดได้ในส่วนของ ASP.NET Database ซึ่งมีตัวคำสั่งดังนี้

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


จากนั้นทำการสร้าง DataSet เพื่อติดต่อกับไฟล์ XML

<script runat="server">
Sub Page_Load
If Not Page.IsPostBack Then
Dim Mycdcatalog=New DataSet
Mycdcatalog.ReadXml(MapPath("testrepeater.xml"))
End If
End Sub


ในการสร้างและเรียกใช้งาน Repeater Control ใน ไฟล์ aspx นั้นจะมีรูปแบบการเรียกใช้งานที่ถูกต้องดังนี้

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


โดยที่ในการแสดงข้อมูลตรง <ItemTemplate> นั้นเราจะใช้คำสั่ง <%#Container.DataItem("ชื่อของฟิลด์ที่จะแสดง")%> ในการแสดงข้อมูลออกมา ดังนี้

<%@ Import Namespace="System.Data" %>
<script runat="server">
sub Page_Load
If Not Page.IsPostBack then
Dim Mycdcatalog=New DataSet
Mycdcatalog.ReadXml(MapPath("testrepeater.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>


ผมมี 3 ตัวอย่างให้ดูนะครับ
Repeater control

Repeater control with (สลับสีของแถวในการแสดง)

Repeater control with (แทรกเส้นกั้นระหว่างแถว)

 
Tutorials ASP.NET
  ASP.NET
      What's ASP.NET
      Install ASP.NET
      Install IIS
      Set up IIS
      Virtual Directory
      การใช้งาน ASP.NET
      การแยกประโยคคำสั่ง
      คำสั่งในการตัดสินใจ
      คำสั่งในการทำซ้ำ
      Array
         Static Array
         Dynamic Array
      Collection
         ArrayList
         Hashtable
         SortedList
  ASP.NET Forms
      TextBox
      Button
      ViewState
  การรับส่งข้อมูล
      QueryString
      Form
      Property
  Controls
      HTML Controls
      Basic Web Controls
      Validation Controls
      DataList Controls
         XML Files
         Repeater
         Datalist
      Custom Control
  ASP.NET Database
      ADO.NET
      DataSet
         ติดต่อฐานข้อมูล
         เปิดฐานข้อมูล
         เลือกข้อมูล
         ใช้ข้อมูลที่เลือก
         ปิดการติดต่อ
         Example DataSet
      DataReader
         ติดต่อฐานข้อมูล
         เปิดฐานข้อมูล
         เลือกข้อมูล
         ใช้ข้อมูลที่เลือก
         ปิดการติดต่อ
         Example DataReader
      DB Connection


[ Users browsing page: 1 :Guest(s) ]

บทความต่างๆที่ปรากฏขึ้นใน ASPThai.Net เป็นบทความที่ได้มาจากการค้นคว้าและหาข้อมูลของผู้จัดทำซึ่งบทความในบางส่วนนั้นได้แหล่งข้อมูลมาจากหนังสือและบทความทางอินเตอร์เน็ต
ถ้าบทความที่ปรากฏขึ้นใน ASPThai.Net ผิดพลาดประการใดก็ขออภัยมา ณ ที่นี้ด้วย
"ทีมงาน ASPThai.Net อนุญาตให้นำบทความต่างๆภายใน ASPThai.Net ไปพิมพ์เพื่อเผยแพร่ต่อไปได้ และโปรดสร้างลิงค์ไปยัง http://www.aspthai.net ด้วยครับ"


This Site is Powered By ASPThai.Net Full Edition v1.
Copyright ฉ 2002-2005 ASPThai.Net All rights reserved.

.