how to connect xml file to asp.net dropdown list cascading

Posted by reddysankark-13471 under ASP.NET on 2/5/2012 | Points: 10 | Views : 5128 | Status : [Member] | Replies : 2
hi
i will develop one software for that i need one solution that is i have xml file with country,state, district for this xml file i will bind with dropdownlist .
from this dropdownlist1 act as country,after selecting the country next states will be shown in dropdownlist2.can u please tell me the code

sankarreddy


Responses

Posted by: Ogipansrk on: 2/6/2012 [Member] Starter | Points: 25

Up
0
Down
Please check this:
http://naspinski.net/post/Cascading-DropDowns-using-XML-and-LINQ.aspx

reddysankark-13471, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pradeepkumar417 on: 2/6/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Try this code. It will work.

XML File:

<?xml version="1.0" encoding="utf-8" ?>
<root>
<country id="india">
<state>Andhra</state>
<state>Tamilnadu</state>
</country>
<country id="America">
<state>Washingtone</state>
<state>Test</state>
</country>
</root>

Code:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("XMLFile.xml"));
DropDownList1.DataTextField = "id";
DropDownList1.DataValueField = "country_Id";
DropDownList1.DataSource = ds;
DropDownList1.DataBind();
DropDownList1.SelectedIndex = 0;
Bind();
}

}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Bind();
}

private void Bind()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("XMLFile.xml"));
DropDownList2.DataTextField = "state_Text";
DropDownList2.DataValueField = "state_Text";
DataView view = new DataView(ds.Tables["state"]);
view.RowFilter = "country_Id='" + DropDownList1.SelectedValue + "'";
DropDownList2.DataSource = view;
DropDownList2.DataBind();
}


If it is useful don't forget to mark as answer.

Thanks & Regards,
Software Engineer,
Pradeep Kumar

reddysankark-13471, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response