In your .aspx file make sure you include AutoPostBack="True". This will allow the control to notify server when user selected an item in the list
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True"
onselectedindexchanged="ListBox1_SelectedIndexChanged"></asp:ListBox>
<asp:ListBox ID="ListBox2" runat="server" AutoPostBack="True"
onselectedindexchanged="ListBox2_SelectedIndexChanged"></asp:ListBox>
<asp:ListBox ID="ListBox3" runat="server"></asp:ListBox>
In your code behind, you may code how item is being populated into another listbox based on selected item.
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//populate data to ListBox2
}
protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
//populate data to ListBox3
}
Add a script manager and update panel into the .aspx page. Include all ListBoxes into the update panel. Everything in the update panel will be repopulated asynchronously.
Derice
Junior Poster in Training
83 posts since Mar 2007
Reputation Points: 10
Solved Threads: 3
Would this work if I had to extract data from a table in a database?
answer: Sure, just have all the data extraction and insertion into the Listbox done in SelectedIndexChanged event.
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//extract from db and insert into listbox2
}
protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
//extract from db and insert into listbox3
}
should I give value tags to all the items in the drop down and search the table to populate the 2nd dropdown based on the selected value in the first?
answer: It is optional, as long as you can identify which corresponding data to get for the next listbox.
Derice
Junior Poster in Training
83 posts since Mar 2007
Reputation Points: 10
Solved Threads: 3
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//extract from db and insert into listbox2
//you may get your selected value for ListBox1 with ListBox1.SelectedItem.Text / ListBox1.SelectedItem.Value
//based on the selected value extract the corresponding DataTable
//databound your next ListBox2 with
ListBox2.DataSource = DataTable;
ListBox2.DataBind();
}
protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
//extract from db and insert into listbox3
}
Derice
Junior Poster in Training
83 posts since Mar 2007
Reputation Points: 10
Solved Threads: 3