【问题标题】:How I can fill My DropDownList with the XmlDataSource Control in ASP.NET如何在 ASP.NET 中使用 XmlDataSource 控件填充我的 DropDownList
【发布时间】:2012-10-22 09:00:22
【问题描述】:

我想将一个 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 文件填充,但是我可以这样做吗?

【问题讨论】:

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


【解决方案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();
    }
}         

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多