DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Ajax ComboBox

  1. #1
    Join Date
    Oct 2007

    Ajax ComboBox

    I've 3 comboboxes(Country,State,City).
    In this wat i needed is if i click on any particular state it has to filter the cities and display only the cities related to that state.the same with country.
    pls help me out how can i do this?I need the code in asp.net C#
    thnq in adv
    Last edited by lavisamudrala; 12-13-2007 at 02:39 AM.

  2. #2
    Join Date
    Apr 2007
    Sterling Heights, Michigan
    Welcome to DevX

    Well, before you can code that, you need to have ALL these cities stored somewhere. That assumes a database.

    What database are you using?

  3. #3
    Join Date
    Nov 2007
    Not .net, but the logic would be the same. This is adapted from my code, so it may not work 100% without a few tweaks:

    This really isn't AJAX, is it? Seems like regular old DHTML to me. Anyhow, ...

    Assumes that you already have the country, state, city values in some type of arrays named:
    myCountryArray (values: myCountryArray[0] = "US", myCountryArray[1] = "CANADA", etc.)
    myStateArray (values = myStateArray[0] = "US", myStateArray[1] = "OHIO", myStateArray[2] = "US", myStateArray[3] = "IOWA"
    myCityArray, etc.

    1. setup the dropdowns with onchange events:
    <select NAME="lbCountry" onchange="fillState()"></select

    2. setup functions for the onchange events:
    function fillState()
    var CurCountry = new String("");
    var SelectedState = new String("");

    SelectedState = document.all("lbCountry").options[document.all("lbCountry").selectedIndex].value;

    // be sure the drop down boxes are empty before we start filling them.
    for (var i = document.all("lbState").length;i > 0;i--)
    document.all("lbState").options[0] = null;

    //Fill the State dropdown from the new array (loaded from SQL)
    for (var i=0; i < (myStateArray.length / 2); i++)
    CurCountry = myStateArray[i*2]
    OptionValue = new String(myStateArray[i*2+1]);
    OptionDisplay = new String(myStateArray[i*2+1]);
    if (SelectedState.indexOf(CurCountry) != -1) {
    //optional: this checks a session var to see if the item should be "selected"
    if (OptionDisplay == "<%=(String)session.getAttribute("SessStateDesc")%>" || (i == 0) ) {
    var optionHTML = new Option(OptionDisplay, OptionValue, false, true)
    } else {
    var optionHTML = new Option(OptionDisplay, OptionValue, false, false)
    var length = document.all("lbState").length;
    document.all("lbState").options[length] = optionHTML;

Similar Threads

  1. Ajax within Ajax generated code
    By geekdesign in forum AJAX
    Replies: 2
    Last Post: 09-16-2007, 08:38 PM
  2. using an AJAX function to return a value
    By k285715 in forum AJAX
    Replies: 0
    Last Post: 06-18-2007, 11:06 AM
  3. New forum + some AJAX links
    By Lori in forum AJAX
    Replies: 0
    Last Post: 02-09-2006, 02:26 PM
  4. comboBox question
    By Stan Shankman in forum .NET
    Replies: 2
    Last Post: 09-07-2001, 03:06 AM
  5. ComboBox Value Disappeared Automatically
    By xiao_john@yahoo.com in forum VB Classic
    Replies: 0
    Last Post: 02-20-2001, 02:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center