带有过滤器绑定到下拉列表的XMLDataSource



我有一个XML

<AddressTypes>
  <AddressType name="OFFICE" value="OFFICE" status="true"/>
  <AddressType name="HOME" value="HOME" status="true"/>
  <AddressType name="PRIVATE" value="PRIVATE" status="false"/>
</AddressTypes>

我将它绑定到像

这样的下拉列表
<asp:DropDownList ID="AddressTypesList" runat="server"
            AppendDataBoundItems="true"
            CssClass="selectbox" 
            DataSourceID="AddressesXMLSource"
            DataTextField="name"
            DataValueField="value">
    <asp:ListItem Text="ALL" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:XmlDataSource ID="AddressesXMLSource" runat="server" 
        DataFile="~/App_Data/AdressTypes.xml" 
        XPath="/AddressTypes/AddressType">
</asp:XmlDataSource>

我得到了这里所有的三个字段。但是我想过滤结果,这样我就可以只填充AddressType,其中status="true"。怎么做呢?

尝试修改XmlDataSource中的XPath,使其包含[@status = 'true'],从而只包含与"true"状态匹配的元素。

您的新xpath字符串看起来像这样:

/AddressTypes/AddressType[ @status = 'true' ]

最新更新