Populating Dropdownlist Using listbox and listbox using dropdown

Sandhyab
Posted by Sandhyab under ASP.NET category on | Points: 40 | Views : 1051
In aspx page take Two dropdowns and one listbox. Here I used xml file to bind the data.
In aspx:
 <div>
<asp:DropDownList ID="DropDown1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Displaylistbox" DataTextField="state" DataValueField="state"></asp:DropDownList>
<asp:ListBox ID="listbox1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DisplaySecondDropDown" DataTextField="city" DataValueField="city"></asp:ListBox>
<asp:DropDownList ID="DropDown2" runat="server" AutoPostBack="true" DataTextField="location" DataValueField="location"></asp:DropDownList>

</div>

Code Behind:
protected void PopulateData()
{
DataTable table = GetData();
DataTable state = table.DefaultView.ToTable(true, "state");
DropDown1.DataSource = state;
DropDown1.DataBind();
}

private DataTable GetData()
{
DataSet dset = new DataSet();
dset.ReadXml(Server.MapPath("~/dd.xml"));
DataTable table = dset.Tables[0];
return table;
}
protected void Displaylistbox(object sender, EventArgs e)
{
DataTable table = GetData();
string state = DropDown1.SelectedValue;
table.DefaultView.RowFilter = "state='" + state + "'";
listbox1.DataSource = table;
listbox1.DataBind();

}
protected void DisplaySecondDropDown(object sender, EventArgs e)
{
DataTable table = GetData();
string state = listbox1.SelectedValue;
table.DefaultView.RowFilter = "city='" + state + "'";
DropDown2.DataSource = table;
DropDown2.DataBind();
}

XML:
<?xml version="1.0" encoding="utf-8" ?>
<countries>
<country state="Please Select" city="Please Select" location="Please Select"></country>
<country state="AP" city="HYD" location="PragathiNagar"></country>
<country state="AP" city="KarimNagar" location="Jagityala"></country>
<country state="AP" city="Nizamabad" location="Bodhan"></country>

<country state="WB" city="KOLKATA" location="SALTLAKE"></country>
<country state="WB" city="KARAKPUR" location="IIT"></country>

</countries>



Thanks & Regards

Comments or Responses

Login to post response