⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aspnet_datalist.asp@output=print

📁 W3Schools tutorial..web designing
💻 ASP@OUTPUT=PRINT
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>ASP.NET The DataList Control</title>
 
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />

<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />

<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>

</head>

<body>

<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>

<h1>ASP.NET - The DataList Control</h1>
<a href="aspnet_repeater.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" /></a>
<a href="aspnet_dbconnection.asp"><img alt="next" border="0" src="../images/btn_next.gif" /></a>
<hr />

<p class="intro">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.</p>
<hr />

<h2>Examples</h2>
<p><a target="_blank" href="showasp.asp@filename=demo_datalist1">DataList control</a></p>
<p><a target="_blank" href="showasp.asp@filename=demo_datalist2">DataList control with styles</a></p>
<p><a target="_blank" href="showasp.asp@filename=demo_datalist3">DataList control with &lt;AlternatingItemTemplate&gt;</a></p>
<hr />

<h2>Bind a DataSet to a DataList Control</h2>

<p>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. 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.</p>

<p>We will use the following XML file in our examples (&quot;cdcatalog.xml&quot;):</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;</pre>
<pre>&lt;catalog&gt;
&lt;cd&gt;
&lt;title&gt;Empire Burlesque&lt;/title&gt;
&lt;artist&gt;Bob Dylan&lt;/artist&gt;
&lt;country&gt;USA&lt;/country&gt;
&lt;company&gt;Columbia&lt;/company&gt;
&lt;price&gt;10.90&lt;/price&gt;
&lt;year&gt;1985&lt;/year&gt;
&lt;/cd&gt;
&lt;cd&gt;
&lt;title&gt;Hide your heart&lt;/title&gt;
&lt;artist&gt;Bonnie Tyler&lt;/artist&gt;
&lt;country&gt;UK&lt;/country&gt;
&lt;company&gt;CBS Records&lt;/company&gt;
&lt;price&gt;9.90&lt;/price&gt;
&lt;year&gt;1988&lt;/year&gt;
&lt;/cd&gt;
&lt;cd&gt;
&lt;title&gt;Greatest Hits&lt;/title&gt;
&lt;artist&gt;Dolly Parton&lt;/artist&gt;
&lt;country&gt;USA&lt;/country&gt;
&lt;company&gt;RCA&lt;/company&gt;
&lt;price&gt;9.90&lt;/price&gt;
&lt;year&gt;1982&lt;/year&gt;
&lt;/cd&gt;
&lt;cd&gt;
&lt;title&gt;Still got the blues&lt;/title&gt;
&lt;artist&gt;Gary Moore&lt;/artist&gt;
&lt;country&gt;UK&lt;/country&gt;
&lt;company&gt;Virgin records&lt;/company&gt;
&lt;price&gt;10.20&lt;/price&gt;
&lt;year&gt;1990&lt;/year&gt;
&lt;/cd&gt;
&lt;cd&gt;
&lt;title&gt;Eros&lt;/title&gt;
&lt;artist&gt;Eros Ramazzotti&lt;/artist&gt;
&lt;country&gt;EU&lt;/country&gt;
&lt;company&gt;BMG&lt;/company&gt;
&lt;price&gt;9.90&lt;/price&gt;
&lt;year&gt;1997&lt;/year&gt;
&lt;/cd&gt;
&lt;/catalog&gt;</pre>
  </td></tr>
</table>
<p>Take a look at the XML file: <a target="_blank" href="cdcatalog.xml">cdcatalog.xml</a></p>

<p>First, import the &quot;System.Data&quot; 
namespace. We need this namespace to work with DataSet objects. Include the 
following directive at the top of an .aspx page:</p>

<table class="ex" border="1" width="100%">
<tr><td>
<pre>&lt;%@ Import Namespace=&quot;System.Data&quot; %&gt;</pre>
  </td></tr>
</table>

<p>Next, create a DataSet for the XML file and load the XML file into the DataSet when the page is first loaded:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre>&lt;script runat=&quot;server&quot;&gt;
sub Page_Load
if Not Page.IsPostBack then
  dim mycdcatalog=New DataSet
  mycdcatalog.ReadXml(MapPath(&quot;cdcatalog.xml&quot;))
end if
end sub</pre>
  </td></tr>
</table>

<p>Then we create a DataList in an .aspx page. The contents of the &lt;HeaderTemplate&gt; 
element are rendered first and only once within the output, then the contents of the &lt;ItemTemplate&gt; 
element are repeated for each 
&quot;record&quot; in the DataSet, and last, the contents of the &lt;FooterTemplate&gt; 
element are rendered once within the output:</p>

<table class="ex" border="1" width="100%">
<tr><td>
<pre>&lt;html&gt;
&lt;body&gt;</pre>
<pre>&lt;form runat=&quot;server&quot;&gt;
&lt;asp:DataList id=&quot;cdcatalog&quot; runat=&quot;server&quot;&gt;</pre>
<pre>&lt;HeaderTemplate&gt;
...
&lt;/HeaderTemplate&gt;</pre>
<pre>&lt;ItemTemplate&gt;
...
&lt;/ItemTemplate&gt;</pre>
<pre>&lt;FooterTemplate&gt;
...
&lt;/FooterTemplate&gt;</pre>
<pre>&lt;/asp:DataList&gt;
&lt;/form&gt;</pre>
<pre>&lt;/body&gt;
&lt;/html&gt;</pre>
  </td></tr>
</table>

<p>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 &lt;HeaderTemplate&gt; 
that contains the header of the table, an &lt;ItemTemplate&gt; that contains the 
data items to display, and a &lt;FooterTemplate&gt; that contains a text. Note that 
the gridlines attribute of the DataList is set to &quot;both&quot; to display table 
borders:</p>

<table class="ex" border="1" width="100%">
<tr><td>
<pre>&lt;%@ Import Namespace=&quot;System.Data&quot; %&gt;</pre>
<pre>&lt;script runat=&quot;server&quot;&gt;
sub Page_Load
if Not Page.IsPostBack then
  dim mycdcatalog=New DataSet
  mycdcatalog.ReadXml(MapPath(&quot;cdcatalog.xml&quot;))
  cdcatalog.DataSource=mycdcatalog
  cdcatalog.DataBind()
end if
end sub
&lt;/script&gt;</pre>
<pre>&lt;html&gt;
&lt;body&gt;</pre>
<pre>&lt;form runat=&quot;server&quot;&gt;
&lt;asp:DataList id=&quot;cdcatalog&quot;
gridlines=&quot;both&quot; runat=&quot;server&quot;&gt;</pre>
<pre>&lt;HeaderTemplate&gt;
My CD Catalog
&lt;/HeaderTemplate&gt;</pre>
<pre>&lt;ItemTemplate&gt;
&quot;&lt;%#Container.DataItem(&quot;title&quot;)%&gt;&quot; of
&lt;%#Container.DataItem(&quot;artist&quot;)%&gt; -
$&lt;%#Container.DataItem(&quot;price&quot;)%&gt;
&lt;/ItemTemplate&gt;</pre>
<pre>&lt;FooterTemplate&gt;
Copyright Hege Refsnes
&lt;/FooterTemplate&gt;</pre>
<pre>&lt;/asp:DataList&gt;
&lt;/form&gt;</pre>
<pre>&lt;/body&gt;
&lt;/html&gt;</pre>
  </td></tr>
</table>
<br />
<hr />

<h2>Using Styles</h2>

<p>You can also add styles to the DataList control to make the output more fancy:</p>

<table class="ex" border="1" width="100%">
<tr><td>
<pre>&lt;%@ Import Namespace=&quot;System.Data&quot; %&gt;</pre>
<pre>&lt;script runat=&quot;server&quot;&gt;
sub Page_Load
if Not Page.IsPostBack then
  dim mycdcatalog=New DataSet
  mycdcatalog.ReadXml(MapPath(&quot;cdcatalog.xml&quot;))
  cdcatalog.DataSource=mycdcatalog
  cdcatalog.DataBind()
end if
end sub
&lt;/script&gt;</pre>
<pre>&lt;html&gt;
&lt;body&gt;</pre>
<pre>&lt;form runat=&quot;server&quot;&gt;
&lt;asp:DataList id=&quot;cdcatalog&quot;
runat=&quot;server&quot;
cellpadding=&quot;2&quot;
cellspacing=&quot;2&quot;
borderstyle=&quot;inset&quot;
backcolor=&quot;#e8e8e8&quot;
width=&quot;100%&quot;
headerstyle-font-name=&quot;Verdana&quot;
headerstyle-font-size=&quot;12pt&quot;
headerstyle-horizontalalign=&quot;center&quot;
headerstyle-font-bold=&quot;true&quot;
itemstyle-backcolor=&quot;#778899&quot;
itemstyle-forecolor=&quot;#ffffff&quot;
footerstyle-font-size=&quot;9pt&quot;
footerstyle-font-italic=&quot;true&quot;&gt;</pre>
<pre>&lt;HeaderTemplate&gt;
My CD Catalog
&lt;/HeaderTemplate&gt;</pre>
<pre>&lt;ItemTemplate&gt;
&quot;&lt;%#Container.DataItem(&quot;title&quot;)%&gt;&quot; of
&lt;%#Container.DataItem(&quot;artist&quot;)%&gt; -
$&lt;%#Container.DataItem(&quot;price&quot;)%&gt;
&lt;/ItemTemplate&gt;</pre>
<pre>&lt;FooterTemplate&gt;
Copyright Hege Refsnes
&lt;/FooterTemplate&gt;</pre>
<pre>&lt;/asp:DataList&gt;
&lt;/form&gt;</pre>
<pre>&lt;/body&gt;
&lt;/html&gt;</pre>
  </td></tr>
</table>
<br />

<hr />

<h2>Using the &lt;AlternatingItemTemplate&gt;</h2>

<p>You can add an &lt;AlternatingItemTemplate&gt; element after the &lt;ItemTemplate&gt; 
element to describe the appearance of alternating rows of output. You may style 
the data in the &lt;AlternatingItemTemplate&gt; section within the DataList control:</p>

<table class="ex" border="1" width="100%">
<tr><td>
<pre>&lt;%@ Import Namespace=&quot;System.Data&quot; %&gt;</pre>
<pre>&lt;script runat=&quot;server&quot;&gt;
sub Page_Load
if Not Page.IsPostBack then
dim mycdcatalog=New DataSet
mycdcatalog.ReadXml(MapPath(&quot;cdcatalog.xml&quot;))
cdcatalog.DataSource=mycdcatalog
cdcatalog.DataBind()
end if
end sub
&lt;/script&gt;</pre>
<pre>&lt;html&gt;
&lt;body&gt;</pre>
<pre>&lt;form runat=&quot;server&quot;&gt;
&lt;asp:DataList id=&quot;cdcatalog&quot;
runat=&quot;server&quot;
cellpadding=&quot;2&quot;
cellspacing=&quot;2&quot;
borderstyle=&quot;inset&quot;
backcolor=&quot;#e8e8e8&quot;
width=&quot;100%&quot;
headerstyle-font-name=&quot;Verdana&quot;
headerstyle-font-size=&quot;12pt&quot;
headerstyle-horizontalalign=&quot;center&quot;
headerstyle-font-bold=&quot;True&quot;
itemstyle-backcolor=&quot;#778899&quot;
itemstyle-forecolor=&quot;#ffffff&quot;
alternatingitemstyle-backcolor=&quot;#e8e8e8&quot;
alternatingitemstyle-forecolor=&quot;#000000&quot;
footerstyle-font-size=&quot;9pt&quot;
footerstyle-font-italic=&quot;True&quot;&gt;</pre>
<pre>&lt;HeaderTemplate&gt;
My CD Catalog
&lt;/HeaderTemplate&gt;</pre>
<pre>&lt;ItemTemplate&gt;
&quot;&lt;%#Container.DataItem(&quot;title&quot;)%&gt;&quot; of
&lt;%#Container.DataItem(&quot;artist&quot;)%&gt; -
$&lt;%#Container.DataItem(&quot;price&quot;)%&gt;
&lt;/ItemTemplate&gt;</pre>
<pre>&lt;AlternatingItemTemplate&gt;
&quot;&lt;%#Container.DataItem(&quot;title&quot;)%&gt;&quot; of
&lt;%#Container.DataItem(&quot;artist&quot;)%&gt; -
$&lt;%#Container.DataItem(&quot;price&quot;)%&gt;
&lt;/AlternatingItemTemplate&gt;</pre>
<pre>&lt;FooterTemplate&gt;
&amp;copy; Hege Refsnes
&lt;/FooterTemplate&gt;</pre>
<pre>&lt;/asp:DataList&gt;
&lt;/form&gt;</pre>
<pre>&lt;/body&gt;
&lt;/html&gt;</pre>
  </td></tr>
</table>
<br />
<hr />
<a href="aspnet_repeater.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" /></a>
<a href="aspnet_dbconnection.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>

<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -