2013-07-12 111 views
0

我很新的ASP.NET 我下载了这个XLL与文件是这样的无法绑定XML文件到一个下拉列表在asp.net

<?xml version="1.0" encoding="utf-8"?> 
<countries author="Banmeet Singh" title="Country, State-Province selections" 
date="2008-Feb-05"> 
    <country name="Afghanistan"> 
    <state>Badakhshan</state> 
    <state>Badghis</state> 
    <state>Baghlan</state> 
    <state>Balkh</state> 
    <state>Bamian</state> 
    <state>Farah</state> 
    <state>Faryab</state> 
    <state>Ghazni</state> 
    <state>Ghowr</state> 
    <state>Helmand</state> 
    <state>Herat</state> 
    <state>Jowzjan</state> 
    <state>Kabol</state> 
    <state>Kandahar</state> 

而且这样的例子不胜枚举。 现在我要显示在下拉列表中的国家, 所以这里是在C#代码上的Page_Load

 DataSet myDataSet = new DataSet(); 

     myDataSet.ReadXml(Server.MapPath("xml/country_state.xml")); 
     DropDownList1.DataSource = myDataSet; 
     DropDownList1.DataBind(); 
     DropDownList1.DataTextField = "country"; 
     DropDownList1.DataBind(); 

铱给出这个错误 : - DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'state' 请告诉我,我做错了。感谢

好了更新,下面是HTML代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList> 



    </form> 
</body> 
</html> 
+0

请向我们展示DropDownList1的html部分 – saamorim

回答

0

你必须内Dataset

列表绑定到Table所以,你的结合应该更喜欢这个(假设你只有1在Dataset“表)

DropDownList1.DataSource = myDataSet.Tables[0]; 

也可以尝试结合柱像这样:

DropDownList1.DataTextField = ds.Tables[0].Columns["country"].ToString(); 

最后 - 你不需要两次,只需要一次Bind()。设置好所有的属性后通常是一个好地方。

关于您的state错误 - 您确定已发布所有涉及的代码?这应该只是在你尝试访问'状态'时才会出现,而且看起来你并不像你所发布的那样。