Populating a Drop Down List Dynamically

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaveRook
    New Member
    • Jul 2007
    • 147

    Populating a Drop Down List Dynamically

    Hi

    I am using C# and Access databse for a web site.

    I have a drop down list which I need to populate dynamically according to results in a database.

    Example

    There will be 1 drop down list on the page which offers 3 makes of cars:
    Mazda, Honda, Ford

    The user selects this, the page performs the postback and a new box appears beside it offering 5 colours:
    Red, Blue, Green White, Orange

    Now let's pretend Mazda only comes in Red and blue and green.
    Honda only comes Red, Green and White
    For comes in Blue, White and Orange

    This means if the user selects Mazda, after the page does the postback and displays drop down list 2 the only options are Red Blue and Green (etc for the other makes)

    How do I populate the correct options in the drop down list depending on the choice of car. In the past, I have done this manually with if else statements, but is there any easier way?

    Thanks

    Dave
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by DaveRook
    How do I populate the correct options in the drop down list depending on the choice of car. In the past, I have done this manually with if else statements, but is there any easier way?
    When you need to make a logical choice, you have to use a logical control (by this I mean an If statement or select/switch case). It's not magical, it's the simple case of "IF this do that".

    It sounds like you are trying to implement the example for the CascadingDropDo wn control that comes with the Ajax Toolkit.


    If you download the Ajax Toolkit you will see an example of what they did.

    -Frinny

    Comment

    • teddarr
      New Member
      • Oct 2006
      • 143

      #3
      Ajax Control Toolkit

      These controls are awesome, and for the most part, easy to implement. The cascading dropdown is not the easiest, but there are some good examples that can be found with a simple google search.

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        You could also get around this using a different approach than If...Then using some kind of dataset paradigm:

        Dim Ford = {Color.Red, Color.White, Color.Blue}
        Dim Mazda = {Color.Red, Color.Green, Color.Orange}
        Dim Chevrolet = {Color.Red, Color.Green, Color.Blue}
        Dim Makes = {Ford, Mazda, Chevrolet}

        Make the first drop down list list items from the Makes array which lists each of the other arrays by name (otherwise it'll say {Red, White, Blue} etc). Then when you select one, it populates the second drop down list with the items in the selected array.

        Of course, I'd probably go with the AJAX method previously suggested. Just thought I'd throw an alternative approach out there just for the fun of seeing if I could come up with a different solution.

        You've heard the figure of speech "There's many ways to skin a cat".

        Comment

        Working...