我如何用ASP.NET中的XmlDataSource控件填充我的DropDownList

时间:2012-10-22 09:00:23

标签: c# asp.net xml drop-down-menu datasource

我想将xml文件绑定到我的DropDownList,我想使用xmlDataSource控件。

这是我的ascx代码:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SearchControl.ascx.cs" Inherits="Telefonie.SearchControl" %>

    <div>
        <asp:Label runat="server" ID="lblSearch">Suchbegriff</asp:Label>
        <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
        <asp:Label runat="server" ID="lblLocation">Werk</asp:Label>
        <asp:DropDownList ID="LocationDropDown" runat="server">
        </asp:DropDownList>
        <asp:Button ID="btnSearch" runat="server" Text="Suchen" 
            onclick="btnSearch_Click" />
        <asp:XmlDataSource ID="LocationDataSource" runat="server" DataFile="~/App_Data/Werke.xml"></asp:XmlDataSource>
    </div>

这是我的xml文件(此xml文件位于app_data文件夹中)

<?xml version="1.0" standalone="yes" ?>
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="resources">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="Werk" type="xs:string" minOccurs="0" />        
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <resources>
    <Werk>Keine Angabe</Werk>
  </resources>
  <resources>
    <Werk>Germany</Werk>  
  </resources>
  <resources>
    <Werk>Other</Werk>
  </resources>
</NewDataSet>

我希望我的DropDownList填充这个xml文件,但我可以这样做吗?

1 个答案:

答案 0 :(得分:1)

<asp:XmlDataSource ID="databasesSource" runat="server" XPath="/NewDataSet/resources" />
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
       databasesSource.Data = [your xml];
       databasesSource.DataBind();
    }
}