绑定的DropDownList使用XML [英] bind dropdownlist using XML

查看:103
本文介绍了绑定的DropDownList使用XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来ASP.NET,

i am new to ASP.NET,

我想提出国家,州DropDownList的。

i am making Country, state dropdownlist.

有关如:对于特定的国家,我会从XML文件中读取该国的国家

for eg: For particular country, i will read states of that country from XML file.

下面是 XMLFile.xml

<?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>

和code在 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();     
    }
}

错误:数据绑定:'System.Data.DataRowView'不包含名称为国家的属性。

ERROR : DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'country'.

推荐答案

将其绑定到country_text

Bind it to country_text

DropDownListCountry.DataTextField = "country_text";

您在您的数据集中三个表。国家,州和文本。在数据集中持有国值的字段是country_text和多数民众赞成你应该绑定到什么

You have three tables in your dataset. Country,State and Text. The field in dataset holding country value is country_text and thats what you should bind to

这篇关于绑定的DropDownList使用XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆