【问题标题】:bind dropdownlist using XML使用 XML 绑定下拉列表
【发布时间】:2012-04-19 08:50:07
【问题描述】:

我是 ASP.NET 的新手,

我正在制作国家,州下拉列表。

例如:对于特定国家/地区,我将从 XML 文件中读取该国家/地区的状态。

这是我在XMLFile.xml中的代码sn-p

<?xml version="1.0" encoding="utf-8" ?>
<countrys>

  <country>India</country>
  <state>
    <text>Maharashtra</text>
    <text>Kashmir</text>
    <text>Goa</text>   
  </state>

  <country>Sri Lanka</country>
  <state>
    <text>Kanady</text>
    <text>Colombo</text>
    <text>Galle</text> 
  </state>

  <country>Australia</country>
  <state>
    <text>Sydney</text>
    <text>Perth</text>
    <text>Melbourne</text>
  </state>

  <country>South Africa</country>
  <state>
    <text>Capetown</text>
    <text>Johanusburg</text>
    <text>Durban</text>
  </state>
</countrys>

Country.aspx.cs中的代码

   public partial class Country : System.Web.UI.Page
   {
       protected void Page_Load(object sender, EventArgs e)
       {

            if (!IsPostBack)
            {
                LoadDropdown();
            }
     }

    protected void LoadDropdown()
    {
            DataSet ds = new DataSet();
            ds.ReadXml (Server.MapPath("XMLFile.xml"));

            DropDownListCountry.DataTextField = "country";

            DropDownListCountry.DataSource = ds;
            DropDownListCountry.DataBind();
            DropDownListCountry.Items.Insert(0,new ListItem(" Select ","0"));
        }
     }

    protected void DropDownListCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
            string  st = (DropDownListCountry.SelectedIndex).ToString();

             XDocument main = XDocument.Load(@"XMLFile.xml");

        var query = from user in main.Descendants("country_text")
                where st == user.Element("state").Value
                select user;

        DropDownListState.DataSource = query;
        DropDownListState.DataBind();     
    }
}

错误: DataBinding:“System.Data.DataRowView”不包含名为“country”的属性。

【问题讨论】:

    标签: asp.net xml linq drop-down-menu data-binding


    【解决方案1】:

    将其绑定到 country_text

    DropDownListCountry.DataTextField = "country_text";
    

    您的数据集中有三个表。国家、州和文本。数据集中保存国家值的字段是 country_text ,这就是你应该绑定的

    【讨论】:

    • 它工作正常,但在获取 m 时出现此错误,Could not find file 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\XMLFile.xml'.
    • 因为在 selectedindexchanged 事件中您没有使用 Server.MapPath("state.xml")。在加载 XMLFile 的地方使用 Server.MapPath
    • 我试过这个,`DataSet ds = new DataSet(); ds.ReadXml(Server.MapPath("XMLFile.xml")); var query = from user in ds.Descendants("country_text") where st == user.Element("state").Value select user; DropDownListState.DataSource = 查询; DropDownListState.DataBind();`
    • 你怎么能这样做,你试图在 DataSet 上使用 linq to xml,它不会这样工作,你也必须得到编译器错误。你需要这样的东西来开始 XDocument doc = XDocument.Load(Server.MapPath("XMLFile.xml"));
    • 老兄现在我的 LINQ 查询中出现错误`var query = from user in doc where st == user.Element("state").Value select user;`
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多