آموزش WebForm-کنترل Repeater

چاپ

کنترل Repeater برای نمایش لیستی از آیتم های تکراری استفاده می شوند که به یک کنترل متصل می شوند.


اتصال یک DataSet به یک کنترل 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>

ابتدا فضای نام "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 اضافه می کنیم و mycdcatalog DataSet را به کنترل Repeater متصل می کنیم. همچنین کنترل Repeater را با تگ های Html پر می کنیم و آیتم ها را با استفاده از <% ("Container.DataItem("fieldname#%> به بخش <ItemTemplate> متصل می کنیم.

مثال (آموزش WebForm-کنترل Repeater)

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

خروجی کد بالا:

TitleArtistCompanyPrice
Empire Burlesque  Bob Dylan  Columbia  10.90 
Hide your heart  Bonnie Tyler  CBS Records  9.90 
Greatest Hits  Dolly Parton  RCA  9.90 
Still got the blues  Gary Moore  Virgin records  10.20 
Eros  Eros Ramazzotti  BMG  9.90 

استفاده از <AlternatingItemTemplate>

می توانیم از عنصر <AlternatingItemTemplate> بعد از عنصر <ItemTemplate> استفاده کنیم تا در رابطه با ظاهر ردیف های خروجی توضیح دهیم. در مثال زیر هر ردیف جدول با رنگ خاکستری کم رنگ نمایش داده می شود:

مثال (آموزش WebForm-کنترل Repeater)

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

خروجی کد بالا:

TitleArtistCompanyPrice
Empire Burlesque  Bob Dylan  Columbia  10.90 
Hide your heart  Bonnie Tyler  CBS Records  9.90 
Greatest Hits  Dolly Parton  RCA  9.90 
Still got the blues  Gary Moore  Virgin records  10.20 
Eros  Eros Ramazzotti  BMG  9.90 

استفاده از <SeparatorTemplate>

تگ <SeparatorTemplate> می تواند برای ایجاد یک جدا کننده بین هر رکورد استفاده شود. مثال زیر نحوه درج یک خط افقی بین هر ردیف جدول را نمایش می دهد:

مثال (آموزش WebForm-کنترل Repeater)

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

خروجی کد بالا:

TitleArtistCompanyPrice
Empire Burlesque  Bob Dylan  Columbia  10.90 

Hide your heart  Bonnie Tyler  CBS Records  9.90 

Greatest Hits  Dolly Parton  RCA  9.90 

Still got the blues  Gary Moore  Virgin records  10.20 

Eros  Eros Ramazzotti  BMG  9.90