1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

ajax code in asp to fetch city.

Discussion in 'C#' started by vaayaaedu, Aug 19, 2012.

  1. #1
    I need ajax code in asp to fetch city on selecting state.
    vaayaaedu, Aug 19, 2012 IP
  2. Kousha

    Kousha Peon

    Likes Received:
    Best Answers:
    Trophy Points:
    Ajax code is in Javascript and client-side! and ASP/ASP.Net is a server side language. City/States needs database, and ...
    It's not as simple as you said in a sentence
    Kousha, Sep 21, 2012 IP
  3. WeddiGo

    WeddiGo Greenhorn

    Likes Received:
    Best Answers:
    Trophy Points:
    hah, just done that yesterday with Country > State so I'll give you the example with Country>State and you can figure it out for State>City

    I'm using jQuery for Ajax but here's the logic:

    ASP page:

    I use a database to retrieve values depending on a form request and response.write() everything as a string delimited with "," ...a dirty example without a database would be:

    if request.form("country") = "UK" then
    response.write("'Avon','Bedfordshire','Berkshire','Buckinghamshire','Cambridgeshire','Cheshire','Cleveland','Clwyd','Cornwall','Cumberland','Cumbria','Derbyshire','Devon','Dorset','Durham','Dyfed','East Sussex','East Suffolk','Essex','Fife','Gloucestershire','Grampian','Greater London','Greater Manchester','Gwent','Gwynedd','Hampshire','Hereford and Worcester','Hertfordshire','Humberside','Huntingdonshire','Isle of Wight','Kent','Lancashire','Leicestershire','Lincolnshire','Lothian','Louth','Merseyside','Mid Glamorgan','Monaghan','Norfolk','North Yorkshire','Northamptonshire','Northern Ireland','Northumberland','Nottinghamshire','Orkney Islands','Oxfordshire','Powys','Shetland','Shropshire','Somerset','South Glamorgan','South Yorkshire','Staffordshire','Strathclyde','Suffolk','Surrey','Sussex','Tayside','Tyne and Wear','Warwickshire','West Glamorgan','West Midlands','West Sussex','West Yorkshire','Western Isles','Wiltshire','Worcestershire','Yorkshire'")

    elseif request.form("country") = "USA" then
    response.write("'AK', 'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA', 'WI', 'WV', 'WY'")

    end if

    HTML page:
    There are 3 things you need here:

    1. A listener that waits for your drop down list to change
    2. A function that does the AJAX call to your ASP page sending the "country" value and then updates the "State" drop down
    3. A "Country" drop down list and an empty "State" drop down list

    So...in the HEAD of the page add a reference to jQuery and the listener

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
    $(document).ready(function() { 
        $('[id=country]').bind('change', function (event) { 
            var pCountry= $(this).val();
    Code (markup):

    Then add the function that does the AJAX call posting the country variable to your ASp page and updating the drop down:

    <script language="JavaScript" type="text/JavaScript">
    function setState(val){
        $.post("getStates.asp", {country: val}, 
            var list = data.split(",");
            $("#stateDropdown").append("<option value='All'>All</option>");    
            for(var i=0; i<list.length; i++){      
                pName = list[i];
                var dropList = "<option value='" + pName + "'>" + pName + "</option>";    
    Code (markup):
    And finally in the BODY have your drop down lists for Country and State

     <select name="country" id="country">
                  <option value="All">All</option>
                   <option value="United Kingdom">United Kingdom</option>
                  <option value="United States">United States</option>
    <select name="stateDropdown" id="stateDropdown">
                  <option value="All">All</option>
    Code (markup):

    Hope this helps, shout if you need more explanation
    WeddiGo, Sep 22, 2012 IP