Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid reloading all XML data for each repeater - vb.net

I am trying to place a repeater within a repeater using xml data. I have it working exactly as I want, but the method I have used reloads the data for each repeater. I think I need to cast as an XmlNode but I'll be honest - I have no idea where to start.

Here is my code - I'd like to keep everything in the code behind if possible.

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

Any ideas?

like image 445
Tom Avatar asked Nov 14 '22 03:11

Tom


1 Answers

Am I missing something here?

Can't you simply comment/remove your doc.Load(Server.MapPath("~/myxml/bookstore.xml")) out within your itemDB Sub? Since you defined doc "globally" and already loaded it on page load? (by doing that you will already avoid reloading the xml)

That said, I do agree with Caspar that you should rather use the XmlDatasource (especially for its caching abilities), you don't have to use the XmlDatasource within your markup - you can always define it within your code-behind as well - Since you're concerned about people seeing your (asp.net server-side based) markup for some reason...

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

Markup: (Nice thing you'll notice here, is that we bind the second repeater using the source from the first repeater)

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

The implementation using the ListView control (one of my favorite asp.net control) will look something like this: (If there's no books available, it will display the markup within the 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>
like image 135
cstruter Avatar answered Dec 23 '22 07:12

cstruter