+ Reply to Thread
Results 1 to 5 of 5

Thread: Dynamically Linked Drop-Down Boxes

  1. #1
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    Dynamically Linked Drop-Down Boxes

    Hi All,

    ok, i have a couple of questions....i found this code to do the dynamically linked drop down boxes so i won't have to reload my (form), code same as link: New Page 1 and it looks good...just need a little help since i'm not good with js.

    1) when my form (screen) is loaded, it's automatically putting the first Region in the list, is there a way to get it to show (blank space). if not, no biggie.

    2) i'm using region/district, instead of state/city....but from the looks of it, it will pass the region name/ district name....if i add RegionID and DistrictID to the select statement, where do I put this as the value to pass (submit-add to database). In my table, i want to just hold the ID's not the actual name. I'm use to using the option tag with the rs data so I can pass that to the table but i'm having a hard time understanding the equivalent in the js code.

    Thanks Peeps!
    Last edited by Rebelle; September 24th, 2008 at 09:42 AM.

  2. #2
    Barn Enthusiast guddu is on a distinguished road guddu's Avatar
    Join Date
    Jul 2008
    Location
    Oxford UK
    Posts
    471
    Rep Power
    4

    can u post the code which is written by u.so we can find out what u missed?
    Love is physical attraction and mental destruction

  3. #3
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    Hi Guddu,

    sure...here it is...
    Code:
    var Region = new Array(); 
    var District = new Array();
    
    function addRegion(region) {
        v = Region.length; 
        Region.length ++; 
        Region[v] = region; 
        v = District.length; 
        District.length ++; 
        District[region] = new Array(); 
    }
    
    function addDistrict(region,district) {
        v = District[region].length;
        District[region].length ++;
        District[region][v] = district;
    }
    
    function loadRegionList() {
        var ctrlRegion = document.frmSend.Region;
        ctrlRegion.options.length = 0;
        for (i=0;i<Region.length;i++) {
            ctrlRegion.options[i] = new Option(Region[i],Region[i]);
        }
    }
    
    function loadDistrictList() {
        var ctrlRegion = document.frmSend.Region;
        var selRegion = ctrlRegion.options[ctrlRegion.selectedIndex].value;
        var ctrlDistrict = document.frmSend.District;
        ctrlDistrict.options.length = 0;
        for (i=0;i<District[selRegion].length;i++) {
            ctrlDistrict.options[i] = new Option(District[selRegion][i]);
        }
    }
    
    on the body tag:
    Code:
    <BODY onload="loadRegionList();loadDistrictList();">
    
    then, concatenate is my district name:
    Code:
    <%
    strSQL = "Select RegionNm, Concatenate from vwReg_Dist Order by RegionNm,Concatenate"
    set rs = conn.execute(strSQL)
    
    if rs.eof then
        response.write("No Region Found")
        rs.close
        set rs=nothing
        response.end
    end if
    
    strRegion = ""
    strDistrict = ""
    do until rs.eof
        if not rs("RegionNm") = strRegion then 
            response.write("<script>addRegion('" & rs("RegionNm") & "')</script>")
            response.write("<script>addDistrict('" & rs("RegionNm") & "','" & rs("Concatenate") & "')</script>")
            strRegion = rs("RegionNm")
            strDistrict = rs("Concatenate")
        else 
            if not rs("Concatenate") = strDistrict then
                response.write("<script>addDistrict('" & rs("RegionNm") & "','" & rs("Concatenate") & "')</script>")
                strDistrict = rs("Concatenate")
            end if
        end if
        rs.MoveNext
    loop
    rs.close
    set rs=nothing
    set conn=nothing
    %>
    
    my table info:
    Code:
    <tr>
                <td CLASS="fieldLblB" WIDTH="120">Region:</td>
                <td>
                    <select size="1" name="Region" onChange="loadDistrictList();">
                    </select>
                </td>
            </tr>
            <tr>
                <td CLASS="fieldLblB" WIDTH="120">District</td>
                <td>
                    <select size="1" name="District">
                    </select>
                </td>
            </tr>
    
    unclear how the javascript equals asp, normally my <select> tag would have an <option> following calling rs value. also, since i don't have a blank record, how do i get something like <option value="">Select Region</option> as the default instead of it defaulting to the first Region Name,DistrictName? Is this possible?

    Thanks!
    Last edited by Rebelle; September 25th, 2008 at 10:04 AM.

  4. #4
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    Ok....I added RegionID and DistrictID to the code in above post and now when i view source of my form page i can see (regionname,regionid) and (regionname,district,regionid,districtid).

    I'll see how it works when adding only the "IDs" to the database....i'll be back if i can't get it.

    Any clues on making default blank?

  5. #5
    Barn Legend Rebelle will become famous soon enough Rebelle's Avatar
    Join Date
    Mar 2008
    Posts
    1,522
    Rep Power
    5

    Hi All,

    I am passing the IDs to the function addRegion and addDistrict but still not sure how to grab it to write to database...anyway...

    I was able to get the other thingy....so now instead of default to the first region name and first district name, it now reads Choose One. see below...old and new code. bold lines is what changed.

    Old, no "Choose One" default:
    Code:
    function loadRegionList() {
        var ctrlRegion = document.frmSend.Region;
    	ctrlRegion.options.length = 0;
        		for (i=0;i<Region.length;i++) {
           	ctrlRegion.options[i] = new Option(Region[i],Region[i]);
        }
    }
    
    function loadDistrictList() {
        var ctrlRegion = document.frmSend.Region;
        var selRegion = ctrlRegion.options[ctrlRegion.selectedIndex].value;
        var ctrlDistrict = document.frmSend.District;
    	ctrlDistrict.options.length = 0;
    		for (i=0;i<District[selRegion].length;i++) {
    	ctrlDistrict.options[i] = new Option(District[selRegion][i]);
        }
    }
    
    New, with default "Choose One":
    Code:
    function loadRegionList() {
        var ctrlRegion = document.frmSend.Region;
    	ctrlRegion.options[0] = new Option("Choose One:","");
    		for (i=1;i<=Region.length;i++) {
    	ctrlRegion.options[i] = new Option(Region[i-1],Region[i-1]);    }
    }
    
    function loadDistrictList() {
        var ctrlRegion = document.frmSend.Region;
        var selRegion = ctrlRegion.options[ctrlRegion.selectedIndex].value;
        var ctrlDistrict = document.frmSend.District;
    	ctrlDistrict.options[0] = new Option("Choose One:","");
    		for (i=1;i<=District[selRegion].length;i++) {
    	ctrlDistrict.options[i] = new Option(District[selRegion][i-1]);
        }
    }
    
    w00t!

+ Reply to Thread

Similar Threads

  1. drop down problem
    By todd2006 in forum JavaScript Programming
    Replies: 32
    Last Post: July 1st, 2008, 05:46 PM
  2. Dynamically Reference Worksheet Cell/Range
    By BLaaaaaaaaaarche in forum Microsoft Office
    Replies: 2
    Last Post: June 18th, 2008, 04:59 PM
  3. dynamically created dropdowns insert to sql
    By peebman2000 in forum .NET Development
    Replies: 14
    Last Post: May 19th, 2008, 12:00 PM
  4. update gridview dynamically
    By peebman2000 in forum .NET Development
    Replies: 27
    Last Post: May 8th, 2008, 11:03 PM
  5. drop down problem
    By todd2006 in forum .NET Development
    Replies: 2
    Last Post: April 23rd, 2008, 12:37 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO