ASP.NET Web Forms – Repeater 控件简介

导读 Repeater 控件用于显示被绑定在该控件上的项目的重复列表。

ASP.NET Web Forms – Repeater 控件简介ASP.NET Web Forms – Repeater 控件简介

绑定 DataSet 到 Repeater 控件

Repeater 控件用于显示被绑定在该控件上的项目的重复列表。Repeater 控件可被绑定到数据库表、XML 文件或者其他项目列表。在这里,我们将演示如何绑定 XML 文件到 Repeater 控件。

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



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

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

<%@ Import Namespace="System.Data" %>

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


sub Page_Load
if Not Page.IsPostBack then
dim mycdcatalog=New DataSet
mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
end if
end sub

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






...


...


...




然后我们添加创建 DataSet 的 脚本,并且绑定 mycdcatalog DataSet 到 Repeater 控件。然后
使用 HTML 标签来填充 Repeater 控件,并通过 <%#Container.DataItem("fieldname")%> 绑定数据项目到 区域内的单元格中:

实例

<%@ Import Namespace="System.Data" %>

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








Title
Artist
Country
Company
Price
Year




<%#Container.DataItem("title")%>
<%#Container.DataItem("artist")%>
<%#Container.DataItem("country")%>
<%#Container.DataItem("company")%>
<%#Container.DataItem("price")%>
<%#Container.DataItem("year")%>








使用

您可以在 元素后添加 元素,用来描述输出中交替行的外观。在下面的实例中,表格每隔一行就会显示为浅灰色的背景:

实例

<%@ Import Namespace="System.Data" %>

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








Title
Artist
Country
Company
Price
Year




<%#Container.DataItem("title")%>
<%#Container.DataItem("artist")%>
<%#Container.DataItem("country")%>
<%#Container.DataItem("company")%>
<%#Container.DataItem("price")%>
<%#Container.DataItem("year")%>




<%#Container.DataItem("title")%>
<%#Container.DataItem("artist")%>
<%#Container.DataItem("country")%>
<%#Container.DataItem("company")%>
<%#Container.DataItem("price")%>
<%#Container.DataItem("year")%>








使用

元素用于描述每个记录之间的分隔符。在下面的实例中,每个表格行之间插入了一条水平线:

实例

<%@ Import Namespace="System.Data" %>

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








Title
Artist
Country
Company
Price
Year




<%#Container.DataItem("title")%>
<%#Container.DataItem("artist")%>
<%#Container.DataItem("country")%>
<%#Container.DataItem("company")%>
<%#Container.DataItem("price")%>
<%#Container.DataItem("year")%>













原文来自: https://www.linuxprobe.com/web-forms-repeater.html

请使用浏览器的分享功能分享到微信等