如何使用Nest Repeater使用单个XMLDataSource?

时间:2011-05-03 21:01:53

标签: .net xml repeater xmldatasource

我想使用共享相同XML数据源的嵌套转发器,其中父转发器将数据源传递给子转发器,因此它不需要为父转发器中的每个数据项重新访问数据源。

父级转发器的XPATH:“/ AdpDeselection / Documents / Document [@ type ='A'] / Funds / Fund [@ cuspid ='1234']”

我在下面的代码中为子Repeater中的DataSource属性添加了什么?

我不想使用OnItemDataBound,因为我觉得它不需要它,但我想我可能错了。

    <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结构......

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

1 个答案:

答案 0 :(得分:8)

经过一些更广泛的挖掘,我实际上找到了答案。这是工作代码:

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

这就像一个没有OnItemDataBound代码的魅力。

The secret sauce is the XPathSelect apparently