Search code examples
c#asp.netsqldatasource

Dropdownlist-hierarchy with SQLdatasource


In my ASP.NET folder i have 3 dropdownboxes who are filled with sorts of categories with 3 different SQLdatascources. Each dropdownlist depends on the one above it.
The intention of this thing is that after selecting something in dropdownlist1, the datasource in dropdownlist2 changes. So the data in dropdown2 depend on the selected value in dropdown1. And the data in dropdown3 depend on the selected value in dropdown2.
It all works nice and dandy a first time. But when I selected a value in dd1 and after this a value in dd2, it starts to fail.
When I change the value a second time in dropdown1 for example, the other dropdownlists don't alter.

Thx in advance for replies


Solution

  • To do this kind of thing, you need to make sure your top level drop down list is only populated once, so put it in the page load with a !isPostback around it. Then attach a OnSelectedIndexChanged() event on that top level dropdownlist, and in it, be sure to clear the items in the second level dropdown list before setting it's new datasource.

    Then a OnSelectedIndexChanged() on the second level dropdown list, and be sure to clear the third before populating it.

    Then the third doesn't need any events.