我正在尝试使用xml数据在转发器中放置转发器。我让它完全按照我的意愿工作,但我使用的方法为每个转发器重新加载数据。我想我需要扮演一个XmlNode,但我会说实话 - 我不知道从哪里开始。
这是我的代码 - 如果可能,我想在代码中保留所有内容。
<script runat="server">
Public doc As New XmlDocument()
Public Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
If Not Page.IsPostBack then
doc.Load(Server.MapPath("~/myxml/bookstore.xml"))
Dim nodes As XmlNodeList = doc.SelectNodes("Bookings/Booking[@CLIENT_NO='SA33762']")
rpMyRepeater.DataSource = nodes
rpMyRepeater.DataBind()
End If
End Sub
Protected Sub itemDB(ByVal s As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim rpt As Repeater = CType(e.Item.FindControl("books"), Repeater)
If Not (rpt Is Nothing) Then
doc.Load(Server.MapPath("~/myxml/bookstore.xml"))
Dim nodes2 As XmlNodeList = doc.SelectNodes("Bookings/Booking[@CLIENT_NO='SA33762']/Products/Book")
rpt.DataSource = nodes2
rpt.DataBind()
End If
End If
End Sub
</script>
有什么想法吗?
答案 0 :(得分:1)
我在这里遗漏了什么吗?
你不能简单地在你的itemDB Sub中注释/删除你的doc.Load(Server.MapPath(“〜/ myxml / bookstore.xml”))吗?既然你定义了doc“global”并且已经在页面加载时加载了它? (通过这样做,你已经避免重新加载xml)
那就是说,我同意Caspar你应该使用XmlDatasource (特别是它的缓存功能),你不必在你的标记中使用XmlDatasource - 你总是可以定义它也在你的代码隐藏中 - 因为你担心人们出于某种原因看到你的(基于asp.net服务器端)标记......
e.g。
Public Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim source As New XmlDataSource()
source.DataFile = "~/myxml/bookstore.xml"
source.XPath = "Bookings/Booking[@CLIENT_NO='SA33762']"
rpMyRepeater.DataSource = source
rpMyRepeater.DataBind()
End If
End Sub
标记: (你会注意到的好处是,我们使用第一个转发器的源绑定第二个转发器)
<asp:Repeater ID="rpMyRepeater" runat="server">
<ItemTemplate>
<%#XPath("//Booking/NAME/text()")%>
<asp:Repeater runat="server" ID='books' DataSource='<%#XPathSelect("//Booking/Products/Book") %>'>
<HeaderTemplate>
<h2>
Books</h2>
</HeaderTemplate>
<ItemTemplate>
<p>
Title:
<%#XPath("TITLE/text()")%></p>
<p>
<%#XPath("BOOKCODE/text()")%></p>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
XML
<?xml version="1.0" encoding="utf-8" ?>
<Bookings>
<Booking CLIENT_NO="SA33762">
<NAME>Mr Pf_Test_15033</NAME>
<Products>
<Book>
<TITLE>My Book</TITLE>
<BOOKCODE>12345</BOOKCODE>
</Book>
<Book>
<TITLE>My Book2</TITLE>
<BOOKCODE>123456</BOOKCODE>
</Book>
</Products>
</Booking>
</Bookings>
使用ListView控件(我最喜欢的asp.net控件之一)的实现看起来像这样: (如果没有可用的书籍,它将在EmptyDataTemplate中显示标记)
<asp:Repeater ID="rpMyRepeater" runat="server">
<ItemTemplate>
<%#XPath("//Booking/NAME/text()")%>
<asp:ListView runat="server" ID="books" ItemPlaceholderID="phItems" DataSource='<%#XPathSelect("//Booking/Products/Book") %>'>
<LayoutTemplate>
<h2>
Books</h2>
<asp:PlaceHolder runat="server" ID="phItems"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<p>
Title:
<%#XPath("TITLE/text()")%></p>
<p>
<%#XPath("BOOKCODE/text()")%></p>
</ItemTemplate>
<EmptyDataTemplate>
<p>
Sorry no books available</p>
</EmptyDataTemplate>
</asp:ListView>
</ItemTemplate>
</asp:Repeater>
答案 1 :(得分:0)
使用XmlDatasource代替自定义代码,进行更好的测试,调整,优化并满足您的要求。为什么要写一些已经在框架中的东西? ;)
<asp:xmldatasource
CacheDuration="Infinite"
id="XmlDataSource1"
runat="server"
XPath="Bookings/Booking[@CLIENT_NO='SA33762']" //<-- test this
datafile="~/myxml/bookstore.xml" />