English 中文(简体)
fetch data indownlsit using thelinq
原标题:fetch data in dropdownlsit using linq

i 伙伴关系是新的。 NET,

i 正在形成国家,国家除名。

页: 1 具体国家,一将从XML档案中读到该国的国家。

<>->我无法在我的Droopdownlist......中找到必要的国家。

这里是我的密码,载于XMLFile.xml

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

  <country name="India">
    <state value1="Maharashtra"></state>
    <state value2="Kashmir"></state>
    <state value3="Goa"></state>
  </country>

  <country name="Sri Lanka">
    <state value1="Kanady"></state>
    <state value2="Colombo"></state>
    <state value3="Galle"></state>
  </country>

  <country name="Australia">
    <state valu1e="Sydney"></state>
    <state value2="Perth"></state>
    <state value3="Melbourne"></state>
  </country>

  <country name="South Africa">
    <state value1="Capetown"></state>
    <state value2="Johanusburg"></state>
      <state value3="Durban"></state>
  </country>

</countrys>

缩略语

 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_text";

            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 state in doc.Descendants("countrys").Elements("country")
                    where st == state.Value
                    select state.NextNode;

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

http://www.ohchr.org。 反对不指标的。

<><>>> 感谢:

最佳回答

OK here is the solution: First few changes in the xml, the attribute value1 in the state element should be value for all. So the new XML is:

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

  <country name="India">
    <state value="Maharashtra"></state>
    <state value="Kashmir"></state>
    <state value="Goa"></state>
  </country>

  <country name="Sri Lanka">
    <state value="Kanady"></state>
    <state value="Colombo"></state>
    <state value="Galle"></state>
  </country>

  <country name="Australia">
    <state value="Sydney"></state>
    <state value="Perth"></state>
    <state value="Melbourne"></state>
  </country>

  <country name="South Africa">
    <state value="Capetown"></state>
    <state value="Johanusburg"></state>
    <state value="Durban"></state>
  </country>

</countrys>

如今,协会网页: 你们应该有两条下级名单,即为真正的。

<asp:DropDownList ID="DropDownListCountry" runat="server" AutoPostBack="true"
    onselectedindexchanged="DropDownListCountry_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DropDownListState" runat="server" AutoPostBack="true"
    onselectedindexchanged="DropDownListState_SelectedIndexChanged">
</asp:DropDownList>

Now in the Code behind:
Call LoadCountryDropDown to populate Country - I am using LINQ to XML here as well instead of data set

protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                LoadCountryDropDown();
            }

        }
        void LoadCountryDropDown()
        {
            XDocument doc = XDocument.Load(Server.MapPath("test.xml"));

            DropDownListCountry.DataSource = from t in doc.Descendants("countrys").Elements("country")
                                             select new
                                             {
                                                 Name = t.Attribute("name").Value
                                             };

            DropDownListCountry.DataTextField = "Name";
            DropDownListCountry.DataValueField = "Name";
            DropDownListCountry.DataBind();
            DropDownListCountry.Items.Insert(0, new ListItem(" Select ", "0"));
        }

LoadStateDropDown() 居住国按特定指数的变动情况下降:

private void LoadStateDropDown(string p)
    {
        XDocument doc = XDocument.Load(Server.MapPath("test.xml"));

        var statequery = from t in doc.Descendants("countrys").Elements("country")
                                         where t.Attribute("name").Value.Equals(p)
                                         select new
                                         {
                                             State = t.Elements("state").Attributes("value").ToList()
                                         };

        DropDownListState.DataSource = statequery.First().State;
        DropDownListState.DataTextField = "value";
        DropDownListState.DataValueField = "value";
        DropDownListState.DataBind();
        DropDownListState.Items.Insert(0, new ListItem(" Select ", "0"));
    }

www.un.org/spanish/ecosoc 最终,您有剪辑名单的复印机。

 protected void DropDownListCountry_SelectedIndexChanged(object sender, EventArgs e)
        {

            LoadStateDropDown(DropDownListCountry.SelectedValue);
        }
  protected void DropDownListState_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


(Please rename countrys to countries in xml)

问题回答
  XDocument main = XDocument.Load(@"data.xml"); 
        var query = from state in main.Descendants("countrys").Elements("country")
                    where st == state.Value
                    select state.NextNode; 

Just have one method to fill the dropdown. The code will be easier to understand and maintain. Pass null as parameter if there is no selected country (at first page load). According to the xml file above you seem to have written the wrong strings to select the states. There was an error with selectedindex. Use selected value. Hope this helps:

     protected void Page_Load(object sender, EventArgs e)
       {

            if (!IsPostBack)
            {
                LoadDropdown(null);
            }
     }
     protected void LoadDropdown(string selectedCountry)
        {
                XDocument main = XDocument.Load(@"XMLFile.xml");
                if(selectedCountry != null)
                {
                        DropDownListCountry.DataSource = from state in main.Element("countries").Element(selectedConty).Elements("state")
                        where state.Value = selectedCountry
select state.Value;
                }
                else
                {
                        DropDownListCountry.DataSource = null;
                }
                DropDownListCountry.DataBind();
                DropDownListCountry.Items.Insert(0,new ListItem(" Select ","0"));
            }
         }

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

                 LoadDropdown(st);
        }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签