2008-12-10 93 views
1

问候!将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属性做错了?

回答

2

XPathSelect不会返回可以像那样直接绑定的DataSource。就像你有FormView绑定和你的绑定使用XPath(“...”)而不是绑定(“...”)一样,你也有与DropDownList相同的问题。或者用你的属性构建一个标准的数据源,然后将DDL绑定到该属性上,或者使用生成select选项标签或类似东西的ListView滚动你自己的HTML。

-1

请原谅我对ASP的不熟悉,但不应该包含@?

< ASP:DropDownList的ID = “MyDdl” RUNAT = “服务器” 数据源= '<%#XPathSelect( “区域/ *”)%>' DataTextField =” @name “DataValueField =” @value “/>

+0

否。这样做会导致“DataBinding:'System.Xml.XmlElement'不包含名称为”@name“异常的属性。 – Bullines 2008-12-10 20:14:12

相关问题