3

I'd like to use nested repeaters that share the same XML datasource where the parent repeater passes down the datasource to the child repeater so it doesn't need to re-access the datasource for every dataitem in the parent repeater.

XPATH for Parent Repeater: "/AdpDeselection/Documents/Document[@type='A']/Funds/Fund[@cuspid='1234']"

What would I put for the DataSource attribute in the child Repeater in the code below?

I'd prefer not have to use an OnItemDataBound because I don't think it needs it but I guess I could be wrong.

    <asp:XmlDataSource ID="xdsCurrentFunds" runat="server" DataFile="~/App_Data/CustomApps/DeselectOptions.xml" />

    <asp:Repeater ID="rptCurrentFund" runat="server" OnItemDataBound="rptCurrentFund_ItemDataBound" DataSourceID="xdsCurrentFunds">
        <ItemTemplate>
            <div class="CurrentFund"><%# XPath("@name")%></div>
            <asp:HiddenField ID="hdnID" runat="server" Value='<%# XPath("@cuspid")%>' />
            <asp:Repeater ID="rptReplacementFunds" runat="server" DataSource='WHAT SHOULD I PUT HERE TO GET THE DATASOURCE?'>
                <ItemTemplate>
                    <div class="ReplacementFund"><%# XPath("@ticker")%></div>
                </ItemTemplate>
            </asp:Repeater>
        </ItemTemplate>
        <SeparatorTemplate>
            <br />
        </SeparatorTemplate>
    </asp:Repeater>

XML Structure...

<Deselection>
    <Documents>
        <Document type="A">
            <Funds>
                <Fund cuspid="1234" name="CURRENT FUND NUMBER ONE">
                    <ReplacementFunds>
                        <Fund ticker="ABCD" cuspid="56785678">FUND NUMBER ONE</Fund>
                        <Fund ticker="EFGH" cuspid="23452345">FUND NUMBER TWO</Fund>
                    </ReplacementFunds>
                </Fund>
                <Fund cuspid="2345" name="CURRENT FUND NUMBER ONE">
                    <ReplacementFunds>
                        <Fund ticker="HJKL" cuspid="56785678">FUND NUMBER THREE</Fund>
                        <Fund ticker="YUIO" cuspid="23452345">FUND NUMBER FOUR</Fund>
                    </ReplacementFunds>
                </Fund>
        </Document>
    </Documents>
</Deselection>
RichC
  • 7,829
  • 21
  • 85
  • 149
  • You WILL need an OnItemDataBound... In the current setup... But if you use for/foreach-loops instead you don't... And that completely removes the use of repeaters... But then you need to carefully arrange the data in Lists/Arryas in the codebehind... – Sani Huttunen May 03 '11 at 21:05
  • @sani - thanks. However, I have seen it done this way. I'm just not sure how they came up with what goes in the DataSource property because the example I saw didn't show the XML so I couldn't see the logic behind the ASP.NET code. – RichC May 03 '11 at 22:28
  • Just to make one thing clear: You will be better off with Repeaters and OnItemDataBound than rolling your own for/foreach handling. There is a performance hit with repeaters (I've been told) but unless you have a REALLY busy site Repeaters should suffice... – Sani Huttunen May 03 '11 at 22:32
  • And for what you should put in the childs DataSource item it should be the parents datatype property that reflects the ReplacementFunds List/Array. – Sani Huttunen May 03 '11 at 22:40
  • @sani - thanks; can you add that as an answer so that I can accept it to give you credit? – RichC May 04 '11 at 12:51

1 Answers1

8

I actually found my answer after some more extensive digging. Here's the working code:

<asp:XmlDataSource ID="xdsCurrentFunds" runat="server" DataFile="~/App_Data/CustomApps/DeselectOptions.xml" />

<asp:Repeater ID="rptCurrentFund" runat="server" OnItemDataBound="rptCurrentFund_ItemDataBound" DataSourceID="xdsCurrentFunds">
    <ItemTemplate>
        <div class="CurrentFund"><%# XPath("@name")%></div>
        <asp:Repeater ID="rptReplacementFunds" runat="server" DataSource='<%# XPathSelect("ReplacementFunds/*") %>'>
            <ItemTemplate>
                <div class="ReplacementFund"><%# XPath("@ticker")%></div>
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
    <SeparatorTemplate>
        <br />
    </SeparatorTemplate>
</asp:Repeater>

This works like a charm with no OnItemDataBound code required.

The secret sauce is the XPathSelect apparently.

RichC
  • 7,829
  • 21
  • 85
  • 149
  • You say that no `OnItemDataBound`, but in your code there is `OnItemDataBound="rptCurrentFund_ItemDataBound"` - so which part is true? – Mołot Dec 03 '15 at 10:40
  • I don't remember - could have just been commented out / empty function that was just in there when I was trying to figure it all out. – RichC Dec 03 '15 at 15:57