ASP.NET - Repeater na kontrol
- หน้าก่อนหน้า WebForms XML File
- หน้าต่อไป WebForms DataList
Ang Repeater na kontrol ay ginagamit upang ipakita ang listahan ng pinagpapatuloy na mga item, ang mga item na ito ay limitado sa kontrol.
Eksemplo
I-bind ang DataSet sa Repeater na kontrol
Ang Repeater na kontrol ay ginagamit upang ipakita ang listahan ng pinagpapatuloy na mga item, ang mga item na ito ay limitado sa kontrol. Ang Repeater na kontrol ay maaaring ma-bind sa database table, XML file o iba pang listahan ng mga item. Dito, ipapakita namin kung paano i-bind ang XML file sa Repeater na kontrol.
Ginagamit namin ang sumusunod na XML na file ("cdcatalog.xml") sa halimbawa:
<?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>
Ginawa namin na tingnan ang nasabing XML na file:cdcatalog.xml
Unang-una, iimportahin ang pangalawang pangalanang hapagkitaan "System.Data". Kailangan namin ito upang magtrabaho kasama ang DataSet na bagay. Sa itaas ng pahina ng .aspx, kasama ang sumusunod na utos:
<%@ Import Namespace="System.Data" %>
Kasunod na, lumikha ng isang DataSet para sa XML file, at ilagay ang XML file sa DataSet kapag ang pahina ay unang iniladlad:
<script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath("cdcatalog.xml")) end if end sub
Pagkatapos, lumikha kami ng isang Repeater control sa .aspx pahina. Ang nilalaman ng <HeaderTemplate> ay lumilitaw sa output lamang isang beses, habang ang nilalaman ng <ItemTemplate> ay lumilitaw sa bawat "record" sa DataSet, at sa pagtatapos, ang nilalaman ng <FooterTemplate> ay lumilitaw sa output lamang isang beses:
<html> <body> <form runat="server"> <asp:Repeater id="cdcatalog" runat="server"> <HeaderTemplate> ... </HeaderTemplate> <ItemTemplate> ... </ItemTemplate> <FooterTemplate> ... </FooterTemplate> </asp:Repeater> </form> </body> </html>
Pagkatapos, magdagdag kami ng script na maaaring gumawa ng DataSet, at i-bind ang mycdcatalog DataSet sa Repeater control. Ginagamit din namin ang HTML tag upang punuan ang Repeater control, at i-bind ang data item sa <ItemTemplate> section na gamit ang <%#Container.DataItem("fieldname")%> method sa cell:
<%@ 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>
Gamitin ang <AlternatingItemTemplate>
Maaari mong magdagdag ng <AlternatingItemTemplate> sa kahapon ng <ItemTemplate> upang ilarawan ang hitsura ng alternating rows. Sa sumusunod na halimbawa, bawat linya ng talahanayan ay magpakita ng light gray background sa bawat ikatlong linya:
<%@ 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>
Gamitin ang <SeparatorTemplate>
Ang <SeparatorTemplate> elemento ay maaaring gamitin upang ilarawan ang separator sa pagitan ng bawat record. Ang sumusunod na halimbawa ay nagdagdag ng isang horizontal line sa pagitan ng bawat linya ng talahanayan:
<%@ 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>
- หน้าก่อนหน้า WebForms XML File
- หน้าต่อไป WebForms DataList