Xaisoft
Xaisoft

Reputation: 46651

How to effectively filter listboxes based on multiple listboxes and combinations?

I have an aspx page with 5 listboxes. Each listbox can filter the other one, so if the user selects an item in listbox 1, listbox 2, 3, 4, and 5 should be filtered. If the user selects an item in listbox 3, listbox 1, 2, 4, and 5 should be filtered. The filter can go forward and backward. Another scenario would be where I select an item in listbox 1, it filters the others and then I select an item in listbox 2 and it further filters listbox 1 and the others, like a chain effect. I also have to handle multiple selections as well. This can get very messy and I am curious as to if anyone knows a good way to handle this.

Upvotes: 1

Views: 1070

Answers (1)

brendan
brendan

Reputation: 29996

In my experience with issues like this sometimes the easiest approach is to build everything dynamically in code behind.

You start with ListBox 1 on your page and disable/hide the others as appropriate. Then you handle the ItemSelected event on ListBox 1 and add the appropriate items to ListBox 2 and enable/unhide it. And then handle the Item Selected Event on ListBox 2 in a similar matter cascading updates down your page/control.

If necessary you can wrap the list boxes in an update panel so the refreshes do not impact other controls on the page.

Pseudo Code:

aspx:
    <asp:ListBox runat="server" ID="lb1" OnSelectedIndexChanged="OnLB1Change" AutoPostBack="true" >
        <asp:ListItem Text="A" Value="A"></asp:ListItem>
        <asp:ListItem Text="B" Value="B"></asp:ListItem>
    </asp:ListBox>

code behind:

   protected void OnLB1Change(object sender, EventArgs e)
    {
        int val = ((ListBox)sender).SelectedIndex;

        switch (val)
        {
            case 0:
                //set up LB2 for values A
            break;
            case 1:
               //set up LB2 for values B
            break;

        }
    }

Upvotes: 1

Related Questions