repeater加载xml文件.docx
文本预览下载声明
Repeater 控件用于显示重复的项目列表,这些项目被限制在该控件。Repeater 控件可被绑定到数据库表、XML 文件或者其他项目列表。这里,我们将展示如何把 XML 文件绑定到一个 Repeater 控件。我们将在例子中使用下面的 XML 文件(cdcatalog.xml):?xml version=1.0 encoding=ISO-8859-1?catalogcdtitleEmpire Burlesque/titleartistBob Dylan/artistcountryUSA/countrycompanyColumbia/companyprice10.90/priceyear1985/year/cdcdtitleHide your heart/titleartistBonnie Tyler/artistcountryUK/countrycompanyCBS Records/companyprice9.90/priceyear1988/year/cdcdtitleGreatest Hits/titleartistDolly Parton/artistcountryUSA/countrycompanyRCA/companyprice9.90/priceyear1982/year/cdcdtitleStill got the blues/titleartistGary Moore/artistcountryUK/countrycompanyVirgin records/companyprice10.20/priceyear1990/year/cdcdtitleEros/titleartistEros Ramazzotti/artistcountryEU/countrycompanyBMG/companyprice9.90/priceyear1997/year/cd/catalog请查看该 XML 文件:cdcatalog.xml首先,导入 System.Data 命名空间。我们需要此命名空间与 DataSet 对象一同工作。在 .aspx 页面的顶部包含下面这条指令:%@ Import Namespace=System.Data %接下来,为这个 XML 文件创建一个 DataSet,并把此 XML 文件在页面首次加载时载入 DataSet:script runat=serversubPage_Loadif Not Page.IsPostBack thendimmycdcatalog=New DataSetmycdcatalog.ReadXml(MapPath(cdcatalog.xml))end ifend sub然后我们在 .aspx 页面中创建一个 Repeater 控件。HeaderTemplate 元素中的内容在输出中仅出现一次,而 ItemTemplate 元素的内容会对应 DataSet 中的 record 重复出现,最后,FooterTemplate 的内容在输出中仅出现一次:htmlbodyform runat=serverasp:Repeater id=cdcatalog runat=serverHeaderTemplate.../HeaderTemplateItemTemplate.../ItemTemplateFooterTemplate.../FooterTemplate/asp:Repeater/form/body/html然后我们添加可创建 DataSet 的脚本,并把这个 mycdcatalogDataSet 绑定到 Repeater 控件。我们同样用 HTML 标签来填充这个 Repeater 控件,并通过 %#Container.DataItem(fieldname)% 方法把数据项目绑定到 ItemTemplate 部分内的单元格:%@ Import Namespace=System.Data %script runat=serversubPage_Loadif Not Page.IsPostBack thendimmycdcatalog=New DataSetmycdcatalog.ReadXml(MapPath(cdcatalog.xml))cdcatalog.DataSource=mycdcatalogcdcatalog.DataBind()end ifend sub/scripthtmlbodyform runat=serverasp:Repeater id=cdcatalog runat=serverHeaderTemplatetable border=1 width=100%trthTitle/ththArtist/ththCountry/ththCompany/tht
显示全部