当前位置: 首页 > 网络学院 > 服务端脚本教程 > ASP.NET > 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> |