ASP.NET - DataList kawo

DataList kawo, kama kai Repeater kawo, wanda ke fassara a cikin abubuwan da ke tsawaita kudade a cikin kawo. Amma, DataList kawo ke yi a cikin abubuwan da ke tsawaita kudade a cikin kawo a yi tabbata.

Tattara DataSet zuwa DataList kawo

Kawararinta DataList kawo, kama kai Repeater kawo, wanda ke fassara a cikin abubuwan da ke tsawaita kudade a cikin kawo. Amma, DataList kawo ke yi a cikin abubuwan da ke tsawaita kudade a cikin kawo a yi tabbata. DataList kawo ke iya ganin tabbatar da tabbatar da kudade a cikin tashar da XML, wanda ke iya ganin tabbatar da tashar da XML ko kuma kudade a cikin abubuwan. A yau, ake rarraba hakan kuma yin tabbatar da XML kawo a cikin DataList kawo.

Akan wakilcin kudade a kan baya kusa ("cdcatalog.xml"):

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

  <title>Empire Burlesque</title>
  Bob Dylan
  USA
  Columbia
  10.90
  1985


  Hide your heart
  Bonnie Tyler
  UK
  CBS Records
  9.90
  1988


  Greatest Hits
  Dolly Parton
  USA
  RCA
  9.90
  1982


  Still got the blues
  Gary Moore
  UK
  Virgin records
  10.20
  1990


  Eros
  Eros Ramazzotti
  EU
  BMG
  9.90
  1997


请查看该 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 控件。 元素中的内容在输出中仅出现一次,而 元素的内容会对应 DataSet 中的 "record" 重复出现,最后, 的内容在输出中仅出现一次:

<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 控件。我们同样使用这些元素来填充该 DataList 控件:包含表头的 <HeaderTemplate>,包含要显示的数据项的 <ItemTemplate>,以及包含文本的 <FooterTemplate>。请注意,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 codew3c.com
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>

Gana shi shirin

使用样式

您也可以向 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 codew3c.com
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>

Gana shi shirin

使用 <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>
© codew3c.com
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>

Gana shi shirin