问候!将DropDownList数据源绑定到XPathSelect
我有一些这样的XML:
<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>
我有我的web窗体上的FormView控件中,许多价值观的约束。我想向区子节点绑定到一个DropDownList像这样:
<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" />
不幸的是,而不是看到我所期待的下拉列表中的数据,我看到3项以“区”为文字和没有值。
我知道我的XML是好的,因为出于测试目的,我想在页面上扔Repeater控件,像这样:
<asp:Repeater ID="MyRepeater" runat="server" DataSource='<%# XPathSelect("Areas/*") %>'>
<ItemTemplate>
<%# XPath("@name") %> - <%# XPath("@value") %><br />
</ItemTemplate>
</asp:Repeater>
这工作得很好。
是否有什么,我绑定到下拉列表时,可能与DataTextField和DataValueField属性做错了?
否。这样做会导致“DataBinding:'System.Xml.XmlElement'不包含名称为”@name“异常的属性。 – Bullines 2008-12-10 20:14:12