当前位置:K88软件开发文章中心编程语言.NET.NET02 → 文章内容

ASP.NET DataList 控件

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-14 1:44:56

ASP.NET Web Forms - 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_Loadif Not Page.IsPostBack thendim mycdcatalog=New DataSetmycdcatalog.ReadXml(MapPath("cdcatalog.xml"))end ifend sub然后我们在 .aspx 页面中创建一个 DataList 控件。<HeaderTemplate> 元素中的内容被首先呈现,并且在输出中仅出现一次,而 <ItemTemplate> 元素中的内容会对应 DataSet 中的每条 "record" 重复出现,最后,<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 到 DataList 控件。然后使用包含表头的 <HeaderTemplate>、包含要显示的数据项的 <ItemTemplate> 和包含文本的 <FooterTemplate> 来填充 DataList 控件。请注意,可设置 DataList 的 gridlines 属性为 "both" 来显示表格边框:实例<%@ Import Namespace="System.Data" %><script runat="server">sub Page_Loadif Not Page.IsPostBack thendim mycdcatalog=New DataSetmycdcatalog.ReadXml(MapPath("cdcatalog.xml"))cdcatalog.DataSource=mycdcatalogcdcatalog.DataBind()end ifend 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_Loadif Not Page.IsPostBack thendim mycdcatalog=New DataSetmycdcatalog.ReadXml(MapPath("cdcatalog.xml"))cdcatalog.DataSource=mycdcatalogcdcatalog.DataBind()end ifend 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_Loadif Not Page.IsPostBack thendim mycdcatalog=New DataSetmycdcatalog.ReadXml(MapPath("cdcatalog.xml"))cdcatalog.DataSource=mycdcatalogcdcatalog.DataBind()end ifend 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>© Hege Refsnes</FooterTemplate></asp:DataList></form></body></html>演示实例 ?

ASP.NET DataList 控件