ASP.NET DataList 控件

asp.net web forms - datalist 控件

datalist 控件,类似于 repeater 控件,用于显示绑定在该控件上的项目的重复列表。不过,datalist 控件会默认地在数据项目上添加表格。

绑定 dataset 到 datalist 控件

datalist 控件,类似于 repeater 控件,用于显示绑定在该控件上的项目的重复列表。不过,datalist 控件会默认地在数据项目上添加表格。datalist 控件可被绑定到数据库表、xml 文件或者其他项目列表。在这里,我们将演示如何绑定 xml 文件到 datalist 控件。

在我们的实例中,我们将使用下面的 xml 文件("cdcatalog.xml"):

<?xml version="1.0" encoding="iso-8859-1"?>

<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>

查看这个 xml 文件:cdcatalog.xml

首先,导入 "system.data" 命名空间。我们需要该命名空间与 dataset 对象一起工作。 把下面这条指令包含在 .aspx 页面的顶部:

<%@ import namespace="system.data" %>

接着,为 xml 文件创建一个 dataset,并在页面第一次加载时把这个 xml 文件载入 dataset:

<script runat="server">
sub page_load
if not page.ispostback then
dim mycdcatalog=new dataset
mycdcatalog.readxml(mappath("cdcatalog.xml"))
end if
end sub

然后我们在 .aspx 页面中创建一个 datalist 控件。<headertemplate> 元素中的内容被首先呈现,并且在输出中仅出现一次,而 <itemtemplate> 元素中的内容会对应 dataset 中的每条 "record" 重复出现,最后,<footertemplate> 元素中的内容在输出中仅出现一次:

<html>
<body>

<form runat="server">
<asp:datalist id="cdcatalog" runat="server">

<headertemplate>
...
</headertemplate>

<itemtemplate>
...
</itemtemplate>

<footertemplate>
...
</footertemplate>

</asp:datalist>
</form>

</body>
</html>

然后我们添加创建 dataset 的脚本,并且绑定 mycdcatalog dataset 到 datalist 控件。然后 使用包含表头的 <headertemplate>、包含要显示的数据项的 <itemtemplate> 和包含文本的 <footertemplate> 来填充 datalist 控件。请注意,可设置 datalist 的 gridlines 属性为 "both" 来显示表格边框:

实例

<%@ import namespace="system.data" %>

<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>

<html>
<body>

<form runat="server">
<asp:datalist id="cdcatalog"
gridlines="both" runat="server">

<headertemplate>
my cd catalog
</headertemplate>

<itemtemplate>
"<%#container.dataitem("title")%>" of
<%#container.dataitem("artist")%> -
$<%#container.dataitem("price")%>
</itemtemplate>

<footertemplate>
copyright hege refsnes
</footertemplate>

</asp:datalist>
</form>

</body>
</html>

使用样式

您也可以向 datalist 控件添加样式,让输出更加花哨:

实例

<%@ import namespace="system.data" %>

<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>

<html>
<body>

<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">

<headertemplate>
my cd catalog
</headertemplate>

<itemtemplate>
"<%#container.dataitem("title")%>" of
<%#container.dataitem("artist")%> -
$<%#container.dataitem("price")%>
</itemtemplate>

<footertemplate>
copyright hege refsnes
</footertemplate>

</asp:datalist>
</form>

</body>
</html>

使用 <alternatingitemtemplate>

您可以在 <itemtemplate> 元素后添加 <alternatingitemtemplate> 元素,用来描述输出中交替行的外观。您可以在 datalist 控件内部对 <alternatingitemtemplate> 区域的数据添加样式:

实例

<%@ import namespace="system.data" %>

<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>

<html>
<body>

<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">

<headertemplate>
my cd catalog
</headertemplate>

<itemtemplate>
"<%#container.dataitem("title")%>" of
<%#container.dataitem("artist")%> -
$<%#container.dataitem("price")%>
</itemtemplate>

<alternatingitemtemplate>
"<%#container.dataitem("title")%>" of
<%#container.dataitem("artist")%> -
$<%#container.dataitem("price")%>
</alternatingitemtemplate>

<footertemplate>
&copy; hege refsnes
</footertemplate>

</asp:datalist>
</form>

</body>
</html>


相关文章