w3pop.com :: ÍøÂçѧԺ :: ASP.NET :: ASP.NET - The DataList Control Êý¾ÝÁÐ±í¿Ø¼þ
ASP.NET - The DataList Control Êý¾ÝÁÐ±í¿Ø¼þ
::
The DataList control is, like the Repeater control, used to display a repeated list of items that are bound to the control. However, the DataList control adds a table around the data items by default.
Êý¾ÝÁÐ±í¿Ø¼þ¸úת·¢Æ÷¿Ø¼þÀàËÆ£¬¶¼ÊÇÓÃÀ´ÏÔʾ°ó¶¨µ½¿Ø¼þÉÏÖØ¸´µÄÏîÄ¿ÁС£È»¶øÊý¾ÝÁÐ±í¿Ø¼þÔÚĬÈϵÄÇé¿öÏ»áÌî¼ÓÒ»¸öº¬ÓÐÊý¾ÝÏîµÄ±í¸ñ¡£
DataList control with styles [´øÑùʽµÄ]
DataList control with <AlternatingItemTemplate>
The DataList 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 DataList control.
Êý¾ÝÁÐ±í¿Ø¼þ¿ÉÒÔ°ó¶¨Êý¾Ý±í£¬XMLÎļþ£¬»òÊÇÆäËûµÄÏîÄ¿ÁС£ÏÖÔÚÎÒÃǽ«ÑÝʾÈçºÎ½«Ò»¸öXMLÎļþ°ó¶¨µ½Êý¾ÝÁÐ±í¿Ø¼þÉÏ¡£
We will use the following XML file in our examples ("cdcatalog.xml"):
ÎÒÃǵľÙÀýÖн«»áʹÓÃÏÂÃæµÄÕâ¸ö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 DataList 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:DataList> </body> |
Then we add the script that creates the DataSet and binds the mycdcatalog DataSet to the DataList control. We also fill the DataList control with a <HeaderTemplate> that contains the header of the table, an <ItemTemplate> that contains the data items to display, and a <FooterTemplate> that contains a text. Note that the gridlines attribute of the DataList is set to "both" to display table borders:
È»ºóÎÒÃǼÓÈë½Å±¾À´½¨Á¢Êý¾Ý¼¯£¬²¢½«Êý¾Ý¼¯mycdcatalog°ó¶¨µ½Êý¾ÝÁÐ±í¿Ø¼þÉÏ£¬ÎÒÃÇ»¹Ê¹ÓÃ<HeaderTemplate>À´°üº¬Á˱í¸ñµÄ±êÌ⣬Óà <ItemTemplate> À´ÏÔʾÊý¾ÝÌõÄ¿£¬²¢ÇÒÓÃ<FooterTemplate>À´¼ÓÈëÒ»¶ÎÎÄ×Ö¡£¿ÉÒÔ×¢Òâµ½ Êý¾ÝÁбíÖеÄgridlines ÊôÐÔ±»ÉèÖÃΪ"both"£¬ÕâÑù¿ÉÒÔÏÔʾ±í¸ñµÄ±ß¿ò£º
<%@ Import Namespace="System.Data" %> <script runat="server"> <html> <form runat="server"> <HeaderTemplate> <ItemTemplate> <FooterTemplate> </asp:DataList> </body> |
You can also add styles to the DataList control to make the output more fancy:
Ä㻹¿ÉÒÔΪÊý¾Ý±í¿Ø¼þ¼ÓÈë¶à¸öÑùʽ£¬ÈÃÊä³öµÄÑù×Ó¸ü¼Ó³äÂúÏëÏó£º
<%@ Import Namespace="System.Data" %> <script runat="server"> <html> <form runat="server"> <HeaderTemplate> <ItemTemplate> <FooterTemplate> </asp:DataList> </body> |
You can add an <AlternatingItemTemplate> element after the <ItemTemplate> element to describe the appearance of alternating rows of output. You may style the data in the <AlternatingItemTemplate> section within the DataList control:
Äã¿ÉÒÔÔÚ<ItemTemplate>ÔªËØºó¼ÓÉÏÒ»¸ö <AlternatingItemTemplate>ÔªËØÀ´ÃèÊö½»²æÊä³öÐеÄÍâ¹Û¡£Äã¿ÉÒÔÔÚÊý¾ÝÁбíÖеÄ<AlternatingItemTemplate> ÇøÓòÀïÑùʽ»¯Êý¾Ý£º
<%@ Import Namespace="System.Data" %> <script runat="server"> <html> <form runat="server"> <HeaderTemplate> <ItemTemplate> <AlternatingItemTemplate> <FooterTemplate> </asp:DataList> </body> |
ÆÀÂÛ (0)
All