ASP.NET - Kikabila cha Repeater

Kikabila cha Repeater kinachotumika kuzingatia orodha ya majadiliano ya upya, yaani yaani ya majadiliano hizi ina nafasi kwenye kikabila hiki.

Kumpata DataSet kwenye kikabila cha Repeater

Kikabila cha Repeater kinachotumika kuzingatia orodha ya majadiliano ya upya, yaani yaani ya majadiliano hizi ina nafasi kwenye kikabila hiki. Kikabila cha Repeater kinaweza kuweza kumpata kwenye tabia ya kumbukumbu, faili ya XML au orodha ya majadiliano mengine. Hapa, tunatokanisha kama tunaweza kuweza kumpata faili ya XML kwenye kikabila cha Repeater.

Tunatumiwa kusoma faili hii ya 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>

Tazama faili hii ya XML:cdcatalog.xml

Kwanza, ingia kifungua eneo la "System.Data". Tuhumu neno hilo kufanya kazi na kifaa cha DataSet. Kwenye ukurasa wa .aspx kina chini, kufikia inaamua hii:

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

Kisha tumeungiza DataSet kwa faili ya XML hii, na tumeingiza faili ya XML kwenye kina ya picha ya kwanza kutumia 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

Kisha tumewaandika kina Repeater kwenye eneo .aspx. Muungano wa <HeaderTemplate> haitakayoweza kufikia kwa kila kina, wakati muungano wa <ItemTemplate> haitakayoweza kufikia kwa kila kina wa DataSet, na kisha muungano wa <FooterTemplate> haitakayoweza kufikia kwa kila kina:

<html>
<body>
<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">
<HeaderTemplate>
...
</HeaderTemplate>
<ItemTemplate>
...
</ItemTemplate>
<FooterTemplate>
...
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>

Kisha tumewaingiza skripta yenye uwezo wa kubuni DataSet, na kuiingeza mycdcatalog DataSet kwa kina Repeater. Tunapokea pia HTML tukitumia tukifanya kina Repeater kwa uwanja wa <%#Container.DataItem("fieldname")%> ili kubindisha data item kwenye sehemu ya <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>

Onyesha mifano hii

Tumia <AlternatingItemTemplate>

Wetu tunaweza kuongeza <AlternatingItemTemplate> katika elementi ya <ItemTemplate> ili kuonyesha uingilio wa mabaki ya tabia. Kifupi cha hivi inaonyesha uingilio wa mabaki ya tabia kwa mabaki wa mabaki kila siku:

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

Onyesha mifano hii

Tumia <SeparatorTemplate>

<SeparatorTemplate> elementi inavyotumiwa kudumuza kati ya rekodi. Mfano wa hivi inaonyesha mrua wa mababu kati ya mabaki ya tabia:

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

Onyesha mifano hii