Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old October 17th, 2006, 06:04 AM
degsy degsy is offline
Contributing User
ASP Free God 2nd Plane (6000 - 6499 posts)
 
Join Date: Aug 2005
Location: North East, UK
Posts: 6,191 degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 19 h 41 m 52 sec
Reputation Power: 120
AJAX / ASP Dependent Dropdown

Here is an example of a Dependent Dropdown using Pure ASP.
The addition here is that it also implements AJAX (xmlHTTP) to call the script dynamically without submitting/refreshing the page.

Here is the basic script without any major validation, error messages or other form elements.
http://computer-helpforum.com/asp/a...nt_dropdown.asp

The example uses car manufacturers and models

Here is the the first dropdown using a standard recordset loop to popuplate the dropdown.
asp Code:
Original - asp Code
  1. <form name="form1" method="get" action="<%=Request.ServerVariables("SCRIPT_NAME")%>">
  2.   <select name="sMan" id="sMan" onchange="ajaxcombo('sMan','content_model')">
  3.   <option value="-1">Choose Manufacturer</option>
  4.     <%
  5.     Set rsMan=Server.CreateObject("ADODB.recordset")
  6.     rsMan.Open "SELECT manufacturer_id, manufacturer_name FROM manufacturer", conn
  7.         While Not rsMan.EOF
  8.             opt = "<option value=""" & rsMan("manufacturer_id") & """"
  9.             If CStr(rsMan("manufacturer_id")) = CStr(sMan) Then
  10.                 opt = opt & " selected"
  11.             End If
  12.             opt = opt & ">" & rsMan("manufacturer_name") & "</option>" & vbCRLF
  13.             Response.Write opt
  14.             rsMan.MoveNext
  15.         Wend
  16.     rsMan.Close
  17.     %>
  18.   </select>
  19.   <noscript><input type="submit" name="GetModels" value="Get Models"></noscript>
  20.  
  21. <span id="content_model"><%Call getModels(sMan)%></span>


Three main features of this code are
Code:
onchange="ajaxcombo('sMan','content_model')

This is the Ajax code that calls the dynamic script to populate the second dropdown.

Code:
<noscript><input type="submit" name="GetModels" value="Get Models"></noscript>

This code shows a submit button if Javascript (AJAX) is disabled.

Code:
<span id="content_model"><%Call getModels(sMan)%></span>

This calls the Sub that populates the second dropdown.



asp Code:
Original - asp Code
  1. Sub getModels(manID)
  2.     sql = "SELECT model.model_id, model.model_name"
  3.     sql = sql & " FROM model INNER JOIN (manufacturer INNER JOIN man_mod ON manufacturer.manufacturer_id = man_mod.manufacturer_id) ON model.model_id = man_mod.model_id"
  4.     sql = sql & " WHERE (((man_mod.manufacturer_id)=" & manID & "))"
  5.     sql = sql & " ORDER BY model.model_name"
  6.  
  7.     Response.Write "<select name=""sMod"" onchange=""this.form.submit()"""
  8.     If manID = -1 Then
  9.         Response.Write " disabled"
  10.     End If
  11.     Response.Write ">" & vbCRLF
  12.    
  13.     Response.Write "<option value=""-1"">Choose"
  14.     If manID = -1 Then
  15.         Response.Write " Manufacturer First"
  16.     Else
  17.     Response.Write " Model"
  18.     End If
  19.     Response.Write "</option>" & vbCRLF
  20.    
  21.     If manID > 0 Then
  22.         Set rsMod=Server.CreateObject("ADODB.recordset")
  23.        
  24.    
  25.         rsMod.Open sql, conn
  26.         While Not rsMod.EOF
  27.             opt = "<option value=""" & rsMod("model_id") & """"
  28.             If CStr(rsMod("model_id")) = CStr(sMod) Then
  29.                 opt = opt & " selected"
  30.             End If
  31.             opt = opt & ">" & rsMod("model_name") & "</option>" & vbCRLF
  32.             Response.Write opt
  33.             rsMod.MoveNext
  34.         Wend
  35.         rsMod.Close
  36.     End If
  37.     Response.Write "</select>" & vbCRLF
  38.     Response.Write "<noscript><input type=""submit"" name=""GetModelsDetails"" value=""Get Model Details"""
  39.     If manID = -1 Then
  40.         Response.Write " disabled"
  41.     End If
  42.     Response.Write "></noscript>"
  43. End Sub

This is the Sub to populate the second dropdown. It takes the ID of the Manufacturer submitted to get the associated models.



The script will work using Pure ASP and HTML.
If you want to add the AJAX functionallity then you need a Javascript.

The example here is taken from
http://www.dynamicdrive.com/dynamic...ajaxcontent.htm

asp Code:
Original - asp Code
  1. <script type="text/javascript">
  2.  
  3. /***********************************************
  4. * Dynamic Ajax Content- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
  5. * This notice MUST stay intact for legal use
  6. * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
  7. ***********************************************/
  8.  
  9. var loadedobjects = "";
  10. var rootdomain = "http://" + window.location.hostname;
  11.  
  12. function ajaxpage(url, containerid){
  13. var page_request = false;
  14.     if (window.XMLHttpRequest) // if Mozilla, Safari etc
  15.         page_request = new XMLHttpRequest()
  16.     else if (window.ActiveXObject){ // if IE
  17.         try {
  18.             page_request = new ActiveXObject("Msxml2.XMLHTTP")
  19.         }
  20.         catch (e){
  21.             try{
  22.                 page_request = new ActiveXObject("Microsoft.XMLHTTP")
  23.             }
  24.         catch (e){}
  25.         }
  26.     }
  27.     else
  28.         return false
  29.        
  30.         page_request.onreadystatechange = function(){
  31.         loadpage(page_request, containerid);
  32.     }
  33.  
  34.     page_request.open('GET', url, true);
  35.     page_request.send(null);
  36. }
  37.  
  38. function loadpage(page_request, containerid){
  39.     if(page_request.readyState == 4 && (page_request.status == 200 || window.location.href.indexOf("http") == -1)){
  40.         document.getElementById(containerid).innerHTML = page_request.responseText;
  41.     }
  42. }
  43.  
  44. /***Combo Menu Load Ajax snippet**/
  45. function ajaxcombo(selectobjID, loadarea){
  46. var selectobj = document.getElementById ? document.getElementById(selectobjID) : ""
  47.     if(selectobj != "" && selectobj.options[selectobj.selectedIndex].value != ""){
  48.         url = '<%=Request.ServerVariables("SCRIPT_NAME")%>?method=ajax&sMan=' + selectobj.options[selectobj.selectedIndex].value;
  49.         ajaxpage(url, loadarea);
  50.     }
  51. }
  52. </script>


Code:
url = '<%=Request.ServerVariables("SCRIPT_NAME")%>?method=ajax&sMan=' + selectobj.options[selectobj.selectedIndex].value;

When the script it called it will take the value of the select and append it to the URL. It will then call the Sub dynamically (in the background).


asp Code:
Original - asp Code
  1. If Request("method") = "ajax" Then
  2.     Call getModels(sMan)
  3.     conn.Close()
  4.     Response.End()
  5. End If

If the AJAX script is called then this code will detect it, call the Sub and return the populated dropdown HTML.
The AJAX script will then replace the HTML dynamically in the webpage.



The full script can be seen below.
The script and database are attached.
Attached Files
File Type: zip ajax_dependent_dropdown.zip (17.1 KB, 608 views)
__________________
CyberTechHelp

Reply With Quote
  #2  
Old October 17th, 2006, 06:06 AM
degsy degsy is offline
Contributing User
ASP Free God 2nd Plane (6000 - 6499 posts)
 
Join Date: Aug 2005
Location: North East, UK
Posts: 6,191 degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 19 h 41 m 52 sec
Reputation Power: 120
This is the full script for the Ajax Dependent Dropdown.
Also added is a recordset to display the model details when the model is chosen.

asp Code:
Original - asp Code
  1. <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
  2. <%
  3. Response.Expires = 60
  4. Response.Expiresabsolute = Now() - 1
  5. Response.AddHeader "pragma","no-cache"
  6. Response.AddHeader "cache-control","private"
  7. Response.CacheControl = "no-cache"
  8.  
  9. sMan = -1
  10. If Len(Request("sMan")) > 0 And IsNumeric(Request("sMan")) Then
  11.     sMan = CInt(Request("sMan"))
  12. End If
  13.  
  14. sMod = -1
  15. If Len(Request("sMod")) > 0 And IsNumeric(Request("sMod")) Then
  16.     sMod = CInt(Request("sMod"))
  17. End If
  18.  
  19. Set conn=Server.CreateObject("ADODB.Connection")
  20. conn.Provider="Microsoft.Jet.OLEDB.4.0"
  21. conn.Open Server.MapPath("cars.mdb")
  22.  
  23. If Request("method") = "ajax" Then
  24.     Call getModels(sMan)
  25.     conn.Close()
  26.     Response.End()
  27. End If
  28.  
  29.  
  30. Sub getModels(manID)
  31.     sql = "SELECT model.model_id, model.model_name"
  32.     sql = sql & " FROM model INNER JOIN (manufacturer INNER JOIN man_mod ON manufacturer.manufacturer_id = man_mod.manufacturer_id) ON model.model_id = man_mod.model_id"
  33.     sql = sql & " WHERE (((man_mod.manufacturer_id)=" & manID & "))"
  34.     sql = sql & " ORDER BY model.model_name"
  35.  
  36.     Response.Write "<select name=""sMod"" onchange=""this.form.submit()"""
  37.     If manID = -1 Then
  38.         Response.Write " disabled"
  39.     End If
  40.     Response.Write ">" & vbCRLF
  41.    
  42.     Response.Write "<option value=""-1"">Choose"
  43.     If manID = -1 Then
  44.         Response.Write " Manufacturer First"
  45.     Else
  46.     Response.Write " Model"
  47.     End If
  48.     Response.Write "</option>" & vbCRLF