views:

1225

answers:

2

Greetings!

I have some XML like this:

<Root>
    <MainSection>
        <SomeNode>Some Node Value</SomeNode>
        <SomeOtherNode>Some Other Node Value</SomeOtherNode>
        <Areas>
            <Area someattribute="aaa" name="Alpha" value="0" />
            <Area someattribute="bbb" name="Beta" value="1" />
            <Area someattribute="ddd" name="Delta" value="2" />
        </Areas>
    </MainSection>
</Root>

I have a FormView on my web form in which many of the values are bound. I'd like to bind the Areas child nodes to a DropDownList like so:

<asp:FormView ID="MyFormView" runat="server" DataSourceID="MyXmlDataSource">
    <ItemTemplate>
        <h1><%# XPath("SomeNode")%></h1>
        <asp:Label ID="MyLabel" runat="server" AssociatedControlID="MyDdl" Text='<%# XPath("SomeOtherNode")%>' />
        <asp:DropDownList ID="MyDdl" runat="server" DataSource='<%# XPathSelect("Areas/*") %>' DataTextField="name" DataValueField="value"></asp:DropDownList>
    </ItemTemplate>
</asp:FormView>
<asp:XmlDataSource ID="MyXmlDataSource" runat="server" XPath="Root/MainSection" />

Unfortunately, instead of seeing the data that I'd expect in the dropdown list, I see 3 entries with "Area" as the text and no values.

I know that my XML is ok because for testing purposes, I tried throwing a Repeater control on the page like so:

<asp:Repeater ID="MyRepeater" runat="server" DataSource='<%# XPathSelect("Areas/*") %>'>
    <ItemTemplate>
        <%# XPath("@name") %> - <%# XPath("@value") %><br />
    </ItemTemplate>
</asp:Repeater>

And that worked fine.

Is there something that I'm doing wrong when binding to the dropdown list, perhaps with the DataTextField and DataValueField properties?

A: 

Forgive my lack of familiarity with ASP, but shouldn't your paths include the @?

<asp:DropDownList ID="MyDdl" runat="server" DataSource='<%# XPathSelect("Areas/*") %>' DataTextField="@name" DataValueField="@value" />

Toji
No. Doing so results in a "DataBinding: 'System.Xml.XmlElement' does not contain a property with the name '@name'" exception.
Bullines
+2  A: 

XPathSelect doesn't return a DataSource that can be directly bound like that. Just as you had the FormView bound and your bindings with in it used XPath("...") not Bind("..."), you have the same issue with the DropDownList. Either build a standard DataSource with your attributes and bind the DDL to that, or roll your own HTML with a ListView that generates the select option tags or something like that.

Mufasa