📄 aspnet_datalist.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 <AlternatingItemTemplate></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 ("cdcatalog.xml"):</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><?xml version="1.0" encoding="ISO-8859-1"?></pre>
<pre><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></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 "System.Data"
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><%@ Import Namespace="System.Data" %></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><script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycdcatalog=New DataSet
mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
end if
end sub</pre>
</td></tr>
</table>
<p>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:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><html>
<body></pre>
<pre><form runat="server">
<asp:DataList id="cdcatalog" runat="server"></pre>
<pre><HeaderTemplate>
...
</HeaderTemplate></pre>
<pre><ItemTemplate>
...
</ItemTemplate></pre>
<pre><FooterTemplate>
...
</FooterTemplate></pre>
<pre></asp:DataList>
</form></pre>
<pre></body>
</html></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 <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:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><%@ Import Namespace="System.Data" %></pre>
<pre><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></pre>
<pre><html>
<body></pre>
<pre><form runat="server">
<asp:DataList id="cdcatalog"
gridlines="both" runat="server"></pre>
<pre><HeaderTemplate>
My CD Catalog
</HeaderTemplate></pre>
<pre><ItemTemplate>
"<%#Container.DataItem("title")%>" of
<%#Container.DataItem("artist")%> -
$<%#Container.DataItem("price")%>
</ItemTemplate></pre>
<pre><FooterTemplate>
Copyright Hege Refsnes
</FooterTemplate></pre>
<pre></asp:DataList>
</form></pre>
<pre></body>
</html></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><%@ Import Namespace="System.Data" %></pre>
<pre><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></pre>
<pre><html>
<body></pre>
<pre><form runat="server">
<asp:DataList id="cdcatalog"
runat="server"
cellpadding="2"
cellspacing="2"
borderstyle="inset"
backcolor="#e8e8e8"
width="100%"
headerstyle-font-name="Verdana"
headerstyle-font-size="12pt"
headerstyle-horizontalalign="center"
headerstyle-font-bold="true"
itemstyle-backcolor="#778899"
itemstyle-forecolor="#ffffff"
footerstyle-font-size="9pt"
footerstyle-font-italic="true"></pre>
<pre><HeaderTemplate>
My CD Catalog
</HeaderTemplate></pre>
<pre><ItemTemplate>
"<%#Container.DataItem("title")%>" of
<%#Container.DataItem("artist")%> -
$<%#Container.DataItem("price")%>
</ItemTemplate></pre>
<pre><FooterTemplate>
Copyright Hege Refsnes
</FooterTemplate></pre>
<pre></asp:DataList>
</form></pre>
<pre></body>
</html></pre>
</td></tr>
</table>
<br />
<hr />
<h2>Using the <AlternatingItemTemplate></h2>
<p>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:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><%@ Import Namespace="System.Data" %></pre>
<pre><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></pre>
<pre><html>
<body></pre>
<pre><form runat="server">
<asp:DataList id="cdcatalog"
runat="server"
cellpadding="2"
cellspacing="2"
borderstyle="inset"
backcolor="#e8e8e8"
width="100%"
headerstyle-font-name="Verdana"
headerstyle-font-size="12pt"
headerstyle-horizontalalign="center"
headerstyle-font-bold="True"
itemstyle-backcolor="#778899"
itemstyle-forecolor="#ffffff"
alternatingitemstyle-backcolor="#e8e8e8"
alternatingitemstyle-forecolor="#000000"
footerstyle-font-size="9pt"
footerstyle-font-italic="True"></pre>
<pre><HeaderTemplate>
My CD Catalog
</HeaderTemplate></pre>
<pre><ItemTemplate>
"<%#Container.DataItem("title")%>" of
<%#Container.DataItem("artist")%> -
$<%#Container.DataItem("price")%>
</ItemTemplate></pre>
<pre><AlternatingItemTemplate>
"<%#Container.DataItem("title")%>" of
<%#Container.DataItem("artist")%> -
$<%#Container.DataItem("price")%>
</AlternatingItemTemplate></pre>
<pre><FooterTemplate>
&copy; Hege Refsnes
</FooterTemplate></pre>
<pre></asp:DataList>
</form></pre>
<pre></body>
</html></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 + -