日期:2014-05-17 浏览次数:20543 次
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { XmlDataSource xds = new XmlDataSource(); xds.DataFile = "~/2.xml"; xds.ID = "XmlDataSource1"; form1.Controls.Add(xds); Repeater1.DataSourceID = Repeater3.DataSourceID = "XmlDataSource1"; Repeater1.DataBind(); Repeater3.DataBind(); } protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) { System.Xml.XPath.XPathNavigator nav = ((System.Xml.XPath.IXPathNavigable)e.Item.DataItem).CreateNavigator(); System.Xml.XPath.XPathNodeIterator it = nav.Select("./District"); Repeater Repeater2 = e.Item.FindControl("Repeater2") as Repeater; Repeater2.DataSource = it; Repeater2.DataBind(); } } protected void Repeater3_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item) { IEnumerable em = XPathBinder.Select(e.Item.DataItem, "District") as IEnumerable; em.GetEnumerator(); Repeater Repeater4 = e.Item.FindControl("Repeater4") as Repeater; Repeater4.DataSource = em; Repeater4.DataBind(); } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head> </head> <body> <form id="form1" runat="server"> <h1>方法1</h1> <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound"> <ItemTemplate> <h2 style="background: #ede"> <%#XPath("@text")%></h2> <asp:Repeater ID="Repeater2" runat="server"> <HeaderTemplate><table border="1"></HeaderTemplate><FooterTemplate></table></FooterTemplate> <ItemTemplate> <tr><td><%#XPath("@value")%></td><td><%#XPath("@text")%></td></tr> </ItemTemplate> </asp:Repeater> </ItemTemplate> </asp:Repeater> <h1>方法2</h1> <asp:Repeater ID="Repeater3" runat="server" OnItemDataBound="Repeater3_ItemDataBound"> <ItemTemplate> <h2 style="background: #ede"> <%#XPath("@text")%></h2> <asp:Repeater ID="Repeater4" runat="server"> <HeaderTemplate><table border="1"></HeaderTemplate><FooterTemplate></table></FooterTemplate> <ItemTemplate> <tr><td><%#((System.Xml