ASP.NET - کنترل Repeater
- صفحه قبلی پروندههای XML WebForms
- صفحه بعدی DataList WebForms
کنترل Repeater برای نمایش لیست پروژههای تکراری استفاده میشود، که این پروژهها محدود به این کنترل هستند.
مثال
اتصال DataSet به کنترل Repeater
کنترل Repeater برای نمایش لیست پروژههای تکراری استفاده میشود، که این پروژهها محدود به این کنترل هستند. کنترل Repeater میتواند به جدولهای دادهبانک، فایلهای XML یا لیستهای پروژههای دیگر متصل شود. در اینجا، ما به نمایش نحوه اتصال یک فایل XML به کنترل Repeater میپردازیم.
ما در این مثال از فایل زیر استفاده خواهیم کرد (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" را وارد کنید. ما به این نامفضا برای کار با دادههای DataSet نیاز داریم. در بالای صفحه .aspx شامل این دستور زیر را قرار دهید:
<%@ Import Namespace="System.Data" %>
پس از آن، یک DataSet برای این فایل XML ایجاد میکنیم و این فایل XML را در زمان بارگذاری اولیه صفحه به DataSet بارگذاری میکنیم:
<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 در صفحه .aspx ایجاد میکنیم. محتوای علامت <HeaderTemplate> تنها یک بار در خروجی نمایش داده میشود، محتوای علامت <ItemTemplate> با توجه به رکوردهای DataSet به صورت تکراری نمایش داده میشود و در نهایت، محتوای علامت <FooterTemplate> تنها یک بار در خروجی نمایش داده میشود:
<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> استفاده کنید
شما میتوانید علامت <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>
- صفحه قبلی پروندههای XML WebForms
- صفحه بعدی DataList WebForms