w3pop.com :: ÍøÂçѧԺ :: ASP.NET :: ASP.NET - Repeater Control ת·¢Æ÷¿Ø¼þ
ASP.NET - Repeater Control ת·¢Æ÷¿Ø¼þ
::
The Repeater control is used to display a repeated list of items that are bound to the control.
ת·¢Æ÷¿Ø¼þ¿ÉÓÃÀ´ÏÔʾ¿Ø¼þÖÐÖØ¸´µÄÏîÄ¿ÁÐ±í¡£
Repeater control with <AlternatingItemTemplate>
Repeater control with <SeparatorTemplate>
The Repeater control is used to display a repeated list of items that are bound to the control. The Repeater control may be bound to a database table, an XML file, or another list of items. Here we will show how to bind an XML file to a Repeater control.
ת·¢Æ÷¿Ø¼þ¿ÉÓÃÀ´ÏÔʾ¿Ø¼þÖÐÖØ¸´µÄÏîÄ¿ÁÐ±í¡£×ª·¢Æ÷¿Ø¼þ¿ÉÒÔ°ó¶¨Êý¾Ý±í£¬XMLÎļþ»òÊÇÆäËûµÄÏîÄ¿ÁС£ÕâÀïÎÒÃǽ«ÑÝʾÈçºÎ½«XMLÎļþ°ó¶¨µ½×ª·¢Æ÷¿Ø¼þÉÏ¡£
We will use the following XML file in our examples ("cdcatalog.xml"):
ÎÒÃǽ«ÔÚÎÒÃǵľÙÀýÖÐʹÓÃÏÂÃæÕâ¸öXMLÎļþ£¨”cdcatalog.xml“£©£º
<?xml version="1.0" encoding="ISO-8859-1"?> <catalog> |
Take a look at the XML file: cdcatalog.xml
¿ÉÒÔ¿´Ò»ÏÂÕâ¸öÎļþ£ºcdcatalog.xml
First, import the "System.Data" namespace. We need this namespace to work with DataSet objects. Include the following directive at the top of an .aspx page:
Ê×ÏÈ£¬µ¼Èë"System.Data"ÃüÃû¿Õ¼ä£¬ÎÒÃÇÐèÒªÕâ¸öÃüÃû¿Õ¼äÀ´¸úÊý¾Ý¼¯¶ÔÏóÒ»Æð¹¤×÷¡£ÔÚ.aspxÒ³ÃæµÄ¶¥²¿¼ÓÉÏÒÔÏÂָʾ£º
<%@ Import Namespace="System.Data" %> |
Next, create a DataSet for the XML file and load the XML file into the DataSet when the page is first loaded:
½ÓÏÂÀ´£¬½¨Á¢XMLÎļþµÄÊý¾Ý¼¯£¬²¢ÔÚÒ³ÃæµÚÒ»´Î¼ÓÔØµÄʱºòÔØÈëXMLÎļþµ½Êý¾Ý¼¯ÖУº
<script runat="server"> |
Then we create a Repeater control in an .aspx page. The contents of the <HeaderTemplate> element are rendered first and only once within the output, then the contents of the <ItemTemplate> element are repeated for each "record" in the DataSet, and last, the contents of the <FooterTemplate> element are rendered once within the output:
È»ºóÎÒÃÇÔÚ.aspxÒ³ÃæÀィÁ¢×ª·¢Æ÷¿Ø¼þ¡£ÔÚÊä³öµÄʱºò<HeaderTemplate> ÔªËØµÄÄÚÈÝÖ»»á³ÊÏÖÒ»´Î£¬È»ºó<ItemTemplate>ÔªËØ»áΪÿ¸öÊý¾Ý¼¯ÀïµÄ”¼Ç¼“½øÐÐÖØ¸´³ÊÏÖ£¬×îºó<FooterTemplate>ÔªËØÀïµÄÄÚÈÝÒ²Ö»»áÔÚÊä³öµÄʱºò³ÊÏÖÒ»´Î£º
<html> <form runat="server"> <HeaderTemplate> <ItemTemplate> <FooterTemplate> </asp:Repeater> </body> |
Then we add the script that creates the DataSet and binds the mycdcatalog DataSet to the Repeater control. We also fill the Repeater control with HTML tags and bind the data items to the cells in the<ItemTemplate> section with the <%#Container.DataItem("fieldname")%> method:
È»ºóÎÒÃǼÓÈë½Å±¾À´½¨Á¢Êý¾Ý¼¯ÒÔ¼°½« mycdcatalog Êý¾Ý¼¯°ó¶¨µ½×ª·¢Æ÷¿Ø¼þÉÏ¡£ÎÒÃÇ»¹¿ÉÒÔÍùת·¢Æ÷¿Ø¼þÀï¼ÓÈëHTML±êÇ©ÒÔ¼°Í¨¹ý<%#Container.DataItem("fieldname")%>·½·¨À´¸ø<ItemTemplate>ÇøÓòÀïµÄµ¥Ôª°ó¶¨Êý¾Ý£º
<%@ Import Namespace="System.Data" %> <script runat="server"> <html> <form runat="server"> <HeaderTemplate> <ItemTemplate> <FooterTemplate> </asp:Repeater> </body> |
You can add an <AlternatingItemTemplate> element after the <ItemTemplate> element to describe the appearance of alternating rows of output. In the following example each other row in the table will be displayed in a light grey color:
Äã¿ÉÒÔÔÚ<ItemTemplate> ÔªËØºó¼ÓÉÏ <AlternatingItemTemplate> À´¶ÔÊä³öµÄ½»»¥ÐнøÐÐÍâ¹ÛµÄÃèÊö¡£ÔÚÏÂÃæµÄ¾ÙÀýÖÐÎÒÃÇ»áÈñíÖÐÆäËûÐÐÏÔʾΪ»Ò°×É«£º
<%@ Import Namespace="System.Data" %> <script runat="server"> <html> <form runat="server"> <HeaderTemplate> <ItemTemplate> <AlternatingItemTemplate> <FooterTemplate> </asp:Repeater> </body> |
The <SeparatorTemplate> element can be used to describe a separator between each record. The following example inserts a horizontal line between each table row:
<SeparatorTemplate> ÔªËØ¿ÉÒÔÓÃÀ´ÃèÊöÿ¸ö¼Ç¼¼äµÄ·ÖÀëÆ÷¡£ÒÔϵľÙÀý¾Í»áÔÚû¸ö±í¸ñÐÐÖвåÈëÒ»¸öˮƽÐУº
<%@ Import Namespace="System.Data" %> <script runat="server"> <html> <form runat="server"> <HeaderTemplate> <ItemTemplate> <SeparatorTemplate> <FooterTemplate> </asp:Repeater> </body> |
ÆÀÂÛ (0)
All