首页


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 - Repeater控件

    [前一节] [后一节]

    Repeater控件用来显示被绑定到此控件的数据项的一个循环序列。


    绑定DataSet到Repeater控件

    Repeater控件用来显示被绑定到此控件的数据项的一个循环序列。Repeater控件可以被绑定到数据库表、XML文件或者任何数据项序列。在这里,我们将展示如何绑定XML文件到一个Repeater控件。

    我们将在示例中使用以下这个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页面中创建一个Repeater控件。在输出过程中,首先给出的是<HeaderTemplate>元素中的内容,这部分内容只输出一次,然后是<ItemTemplate>元素中的内容,这部分内容针对DataSet中的每一条“记录”被重复(循环)输出,最后给出的是<FooterTemplate>元素中的内容,仅输出一次:

    <html>
    <body>
    <form runat="server">
    <asp:Repeater id="cdcatalog" runat="server">
    <HeaderTemplate>
    ...
    </HeaderTemplate>
    <ItemTemplate>
    ...
    </ItemTemplate>
    <FooterTemplate>
    ...
    </FooterTemplate>
    </asp:Repeater>
    </form>
    </html>
    </body>

    然后我们加入脚本,创建DataSet并绑定mycdcatalog DataSet到Repeater控件。我们还用HTML标签填充Repeater控件并用<%#Container.DataItem("fieldname")%>方法绑定数据项到<ItemTemplate>部分的单元格中:

    <%@ 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:Repeater id="cdcatalog" runat="server">
    <HeaderTemplate>
    <table border="1" width="100%">
    <tr>
    <th>Title</th>
    <th>Artist</th>
    <th>Country</th>
    <th>Company</th>
    <th>Price</th>
    <th>Year</th>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
    <td><%#Container.DataItem("title")%></td>
    <td><%#Container.DataItem("artist")%></td>
    <td><%#Container.DataItem("country")%></td>
    <td><%#Container.DataItem("company")%></td>
    <td><%#Container.DataItem("price")%></td>
    <td><%#Container.DataItem("year")%></td>
    </tr>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:Repeater>
    </form>
    </html>
    </body>


    使用<AlternatingItemTemplate>

    你可以在<ItemTemplate>元素后面添加一个<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:Repeater id="cdcatalog" runat="server">
    <HeaderTemplate>
    <table border="1" width="100%">
    <tr>
    <th>Title</th>
    <th>Artist</th>
    <th>Country</th>
    <th>Company</th>
    <th>Price</th>
    <th>Year</th>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
    <td><%#Container.DataItem("title")%></td>
    <td><%#Container.DataItem("artist")%></td>
    <td><%#Container.DataItem("country")%></td>
    <td><%#Container.DataItem("company")%></td>
    <td><%#Container.DataItem("price")%></td>
    <td><%#Container.DataItem("year")%></td>
    </tr>
    </ItemTemplate>
    <AlternatingItemTemplate>
    <tr bgcolor="#e8e8e8">
    <td><%#Container.DataItem("title")%></td> <td><%#Container.DataItem("artist")%></td> <td><%#Container.DataItem("country")%></td> <td><%#Container.DataItem("company")%></td> <td><%#Container.DataItem("price")%></td> <td><%#Container.DataItem("year")%></td> </tr> </AlternatingItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:Repeater>
    </form>
    </html>
    </body>


    使用<SeparatorTemplate>

    <SeparatorTemplate>元素用来描述一个介于每条记录之间的分隔符。以下示例在表格每行之间插入一条水平线:

    <%@ 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:Repeater id="cdcatalog" runat="server">
    <HeaderTemplate>
    <table border="0" width="100%">
    <tr>
    <th>Title</th>
    <th>Artist</th>
    <th>Country</th>
    <th>Company</th>
    <th>Price</th>
    <th>Year</th>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
    <td><%#Container.DataItem("title")%></td>
    <td><%#Container.DataItem("artist")%></td>
    <td><%#Container.DataItem("country")%></td>
    <td><%#Container.DataItem("company")%></td>
    <td><%#Container.DataItem("price")%></td>
    <td><%#Container.DataItem("year")%></td>
    </tr>
    </ItemTemplate>
    <SeparatorTemplate>
    <tr>
    <td colspan="6"><hr /></td>
    </tr>
    </SeparatorTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:Repeater>
    </form>
    </html>
    </body>


    [前一节] [后一节]


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

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

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

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

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

    Copyright © Tom.s Online 2003-2004