首页


ASP .NET教程

  • ASP .NET简介
  • ASP与ASP .NET
  • 安装ASP .NET
  • ASP .NET网页
  • ASP .NET控件
  • ASP .NET事件
  • ASP .NET表单

  • ASP .NET Form
  • ASP .NET ViewState
  • asp:TextBox
  • asp:Button
  • ASP.NET绑定

  • 数据绑定
  • ArrayList
  • Hashtable
  • SortedList
  • XML文件
  • asp:Repeater
  • asp:DataList
  • ASP.NET数据库

  • 数据库连接
  • 手册

  • HTML控件
  • Web控件
  • 验证控件
  • 示例

  • ASP .NET示例

  • ASP .NET - 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中的每一条“记录”被重复(循环)输出,最后给出的是<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到Repeater控件。然后我们用以下内容填充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 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>


    [前一节] [后一节]


    本站教程均为老猫根据外文资料翻译整理,将逐步刊出。此版本内容保证国内绝无仅有,由于时间、水平有限,有不妥之处欢迎指正。

    如果能对您有所帮助,敬请赞助,老猫不胜感激!

    1. 网上乞讨:
    在您任何方便的时候,向以下帐号存入10元人民币(当然越多越好)。
    交通银行太平洋卡
    601428 7091 5592604

    2. 广告投放:
    在任何指定页面投放各种形式广告,价格优惠。

    3. 代为宣传:
    以任何形式向您的亲朋好友推荐。

    Copyright © Tom.s Online 2003-2004