Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

synchronized combobox example??

Status
Not open for further replies.

longmatch

Programmer
Nov 1, 2001
406
Dear All:
Anybody has synchronized comboboxes example? I need both comboboxes connected to database and when the item is selected in the first combobox, the contents in the second combobox will change accordingly based upon the selection .

Thanks in advance

Haijun
 
I hope the following code will be helpful. Hmmm, it is a problem of javascrits not asp I think.

If it is not suitalbe, feel free to tell me.

<html>
<head>
<script language = &quot;javascript&quot;>
function set()
{
var option;
option = document.forms[0].select1.selectedIndex;
document.forms[0].select2.selectedIndex = option;
}
</script>
</head>
<body>
<form>
<select name = &quot;select1&quot; onChange = &quot;set()&quot;>
<option>option1</option>
<option>option2</option>
</select>
<br>
<select name = &quot;select2&quot;>
<option>option1</option>
<option>option2</option>
</select>
</form>
</body>
</html>
 
Thank you for your help. If I want to fill the combobox with items in a database, how can I do it? I know how to fill the first combobox based upon a table in database, how to synchronize the second one when first combobox is clicked?

big thanks

Haijun
 
I pulled this ASP example from the Internet when I was attempting to do the same thing you are wanting to do. It has three dropdown boxes that are using ADO to populate each succeeding box. I was able to alter this example for my particular use to succesfully do just what you are wanting to do.

<!-- Dynamic Drop Down. A FrontPageHowTo.com ASP Example -->

<!-- Copy and paste this code into a blank page in FP between the body tags
Import the FpNWind.mdb into your web and set up a database connection called Sample to it -->

<!-- Start First Drop Down -->

<%
Set objDC = Server.CreateObject(&quot;ADODB.Connection&quot;)
objDC.Open Application(&quot;sample_connectionstring&quot;)

Set objRS = objDC.Execute(&quot;Select Distinct Country FROM Suppliers&quot;)
%>
<table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; style=&quot;border-collapse: collapse&quot; bordercolor=&quot;#111111&quot; width=&quot;100%&quot; id=&quot;AutoNumber1&quot;>
<tr>
<td width=&quot;33%&quot;>&nbsp;</td>
<td width=&quot;47%&quot;>
<FORM METHOD=&quot;POST&quot; NAME=&quot;Form1&quot; ACTION=&quot;our_supplier_in.asp&quot;>
<p align=&quot;left&quot;>
<SELECT NAME=&quot;Country&quot; SIZE=&quot;1&quot; ONCHANGE=Form1.submit()>
<option selected><% = Request.Form(&quot;Country&quot;) %></option>
<%
' Continue until we get to the end of the recordset.
Do Until objRS.EOF
' For each record we create a option tag and set it's value to the countr
%>
<OPTION><%= objRS(&quot;Country&quot;) %></OPTION>
<%
' Get next record
objRS.MoveNext
Loop
%>
</SELECT> <b><font face=&quot;Arial&quot; size=&quot;2&quot; color=&quot;#008080&quot;>Choose a Country</font></b></p>
</FORM>
<%

' Close Data Access Objects and free DB variables
objRS.Close
Set objRS = Nothing
objDC.Close
Set objDC = Nothing
%>


<!-- End first Drop Down -->
<!--Second drop down -->

<%
'Some code to hide the second drop down until we make a selection from the first
IF Request.Form(&quot;Country&quot;) = &quot;&quot; Then
Else
'If Country has a value then we get a list of cities for the second drop down
Set objDC = Server.CreateObject(&quot;ADODB.Connection&quot;)
objDC.Open Application(&quot;sample_connectionstring&quot;)

Set objRS = objDC.Execute(&quot;Select City FROM Suppliers WHERE Country = '&quot; & Request.Form(&quot;Country&quot;) & &quot;'&quot;)
%>
<FORM METHOD=&quot;POST&quot; NAME=&quot;Form2&quot; ACTION=&quot;our_supplier_in.asp&quot;>
<p align=&quot;left&quot;>
<font face=&quot;Arial&quot;><font color=&quot;#008080&quot;><b>
<SELECT NAME=&quot;City&quot; SIZE=&quot;1&quot; ONCHANGE=Form2.submit()>
<option selected><% = Request.Form(&quot;City&quot;) %></option>
<%
' Continue until we get to the end of the recordset.
Do Until objRS.EOF
' For each record we create a option tag and set it's value to the city
%>
<OPTION><%= objRS(&quot;City&quot;) %></OPTION>
<%
' Get next record
objRS.MoveNext
Loop
%>
<%
'Set a hidden value in the second form for the Country
'so we can pass it along with the city to the next query
%>
</SELECT></b></font><b><font size=&quot;2&quot; color=&quot;#008080&quot;> Choose a City</font></b><font color=&quot;#008080&quot;><b><input type=&quot;hidden&quot; name=&quot;Country&quot; value=&quot;<% = Request.Form(&quot;Country&quot;) %>&quot;></b></font></font></p>
</FORM>

<%
' Close Data Access Objects and free DB variables
objRS.Close
Set objRS = Nothing
objDC.Close
Set objDC = Nothing
End IF
%>

<!-- Display the records -->

</td>
<td width=&quot;20%&quot;></td>
</tr>
</table>
<table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; style=&quot;border-collapse: collapse&quot; bordercolor=&quot;#111111&quot; width=&quot;100%&quot; id=&quot;AutoNumber2&quot;>
<tr>
<td width=&quot;33%&quot;>&nbsp;</td>
<td width=&quot;47%&quot;>
<%
'Make sure we have submitted a city and don't show results until we do
IF Request.Form(&quot;city&quot;) = &quot;&quot; Then
Else
Set objDC = Server.CreateObject(&quot;ADODB.Connection&quot;)
objDC.Open Application(&quot;sample_connectionstring&quot;)

Set objRS = objDC.Execute(&quot;Select * FROM Suppliers WHERE Country = '&quot; & Request.Form(&quot;Country&quot;) & &quot;' AND City = '&quot; & Request.Form(&quot;city&quot;) & &quot;'&quot;)

'Loop through the database and assign the appropriate values to variables
'that we will use later

Do Until objRS.EOF
CompanyName = objRS(&quot;CompanyName&quot;)
ContactName = objRS(&quot;ContactName&quot;)
Address = objRS(&quot;Address&quot;)
City = objRS(&quot;City&quot;)
Region = objRS(&quot;Region&quot;)
Zip = objRS(&quot;PostalCode&quot;)
Phone1 = objRS(&quot;Phone&quot;)
Fax1 = objRS(&quot;Fax&quot;)
Country = objRS(&quot;country&quot;)
objRS.MoveNext
Loop

objRS.Close
Set objRS = Nothing
objDC.Close
Set objDC = Nothing
%>
</div>
<p align=&quot;left&quot;><b><font face=&quot;Arial&quot; size=&quot;2&quot; color=&quot;#008080&quot;>Our Supplier in: </font>
<font face=&quot;Arial&quot; size=&quot;2&quot; color=&quot;#800000&quot;> <% Response.Write City & &quot;, &quot; & Country %> </font></b>
<br>
<font face=&quot;Arial&quot; size=&quot;1&quot; color=&quot;#008080&quot;>
<%
'Set up the display of the record
Response.Write CompanyName & &quot;<br>&quot;
Response.Write ContactName & &quot;<br>&quot;
Response.Write Address & &quot;<br>&quot;
Response.Write City & &quot;, &quot; & Region & &quot; &quot; & Zip & &quot;<br>&quot;
Response.Write &quot;Phone: &quot; & Phone1 & &quot;<br>&quot;
IF Fax1 <> &quot;&quot; Then
Response.Write &quot;Fax: &quot; & Fax1 & &quot;<br>&quot;
End IF
End IF
%>

</font>
</p>

</td>
<td width=&quot;20%&quot;>&nbsp;</td>
</tr>
</table>


Hope this helps!
Dave
 
Note that CatManDave's example will require you to reload the page and display the data in the different combo boxes. The only way to accomplish what you want without reloading the page is to response.write all the data from the second table in JavaScript arrays. Then when you click an ID in the first combo box, you can use JavaScript to unload all the values in the second and reload the combo box with a different array.

This does require you to response.write ALL possible values for the second combo box into a JavaScript SCRIPT block and send to the browser. If you have a large table, just stick with letting the page reload. Otherwise:

Table1
CatID
CategoryName

Table2
ProductID
ProductName
CatID - as foreign key

<script language=&quot;JavaScript&quot;>
<%
rs.Open &quot;SELECT * FROM Table1&quot;, conn
while not rs.EOF
response.write &quot;var Cat&quot; & rs(&quot;CatID&quot;) & &quot;();&quot; & vbcrlf
SQL = &quot;SELECT * FROM Table2 WHERE CatID=&quot; & CatID
rs2.Open SQL, conn
j = 1
while not rs2.EOF
response.write &quot;Cat&quot; & CatID & &quot;(&quot; & J & &quot;)&quot; = '&quot; & rs2(&quot;ProductName&quot;) & &quot;';&quot; & vbcrlf
rs2.movenext
wend
rs.movenext
wend %>
</script>

This should give you something like so:
<script language=&quot;JavaScript&quot;>
var Cat1();
Cat1(1) = &quot;Red Stamps&quot;;
Cat1(2) = &quot;Blue Stamps&quot;;
var Cat2();
Cat2(1) = &quot;Water&quot;;
Cat2(2) = &quot;Soda&quot;;
</script>

Then whenever someone selects a new CatID from the first combo box, use JavaScript to unload all the values from the second box, and then load all the new ones from eval(&quot;Cat&quot; + CatID + &quot;(0)&quot;) etc into the second combo box.

Again, if you don't mind the page refreshing, then use that way. It makes your page size smaller. I just wanted to make you aware there's a different way to do it.
Harold Blackorby
hblackorby@scoreinteractive.com
St. Louis, MO
 
In the FAQs there are some resources for coding linked combo boxes:

faq333-1507

 
Dear Catmandave:

I would like to use the strategy your provided to solve this problem. But what des the our_supplier_in.asp do?

Thanks

Haijun
 
Ignore it :)

Substitute ACTION=&quot;<your current ASP page name>&quot;
for
ACTION=&quot;our_supplier_in.asp&quot;

in your first form. This will simply force a redisplay of the form again.

For your second form that has your dynamic drop down box built from your first form, use what would normally be the &quot;ACTION=&quot; function you would use if you had only one form and no dynamic drop down boxes.

In other words, if you are going to update a SQL table from your ASP you could use ACTION=&quot;UpdateSql.ASP&quot; here.

Hope this helps,
CatManDave
 
Dear CatmanDave:
In my application, there is only two combo boxes needed. I am planning to use only one form to solve this problem. My strategy is to load the first combo box items directly from database. I use a function to fill the second combo box based on the first combo box selection. The onchange event will be fired when item in the first combo box is selected, which will submit this form. When this form is submited, one function needs to be called to fill the second combo. I am not sure how to call a function in the same page. Give me your suggestions.

Thanks

Haijun
 
There is a way to do that. It's not pretty and the example below is how to do it. I was having a rough time with it asthetically, which is why I decided (since I have the design leeway to do so) to use the two form method instead.

Good Luck!

--------------Dynamically fill combo boxes-------------------------------------
<%
Set rsX = Server.CreateObject(&quot;ADODB.Recordset&quot;)
sQuery = &quot;SELECT Name, ID FROM Manufacturers ORDER BY Name&quot;
rsX.Open sQuery, objConn, adOpenForwardOnly, _
adLockReadOnly, adCmdText
If rsX.EOF Then
Response.Write &quot;No manufacturers.<BR>&quot;
Else
Response.Write &quot;<SELECT NAME=&quot;&quot;manufacturer&quot;&quot; SIZE=15>&quot;
Do Until rsX.EOF
Response.Write &quot;<OPTION VALUE=&quot;&quot;&quot; & rsX(&quot;ID&quot;) & _
&quot;&quot;&quot;>&quot; & rsX(&quot;Name&quot;) & &quot;</OPTION>&quot;
rsX.MoveNext
Loop
Response.Write &quot;</SELECT>&quot;
End If
rsX.Close
Set rsX = Nothing
%>

That puts the manufacturers' names into a 15-row listbox. The trick in this case is that we need to do something when the user selects an element in the listbox. That's done with the &quot;ONCHANGE&quot; event. Although you could write the whole ONCHANGE handler instream in the tag definition, I prefer the idea of pulling it out into its own <SCRIPT> block.

First you have to change the <SELECT> statement to something like this:

<SELECT NAME=&quot;manufacturer&quot; SIZE=15
ONCHANGE=&quot;manuselected();&quot;>

That tells your browser to invoke a client-side script named &quot;manuselected&quot; whenever someone changes the selection in this listbox. What is it you want to do when the user makes a selection? Ah, there's the rub.

Filling The Second Box (JavaScript Background)

JavaScript lets you manipulate the contents of listboxes and combo boxes at run time. Let's take a simple example. Suppose you have a listbox that you want to clear:

First Entry Second Entry Third Entry Fourth Entry Fifth Entry Sixth Entry Seventh Entry Eighth Entry Ninth Entry Tenth Entry Click here to clear the box.

Assuming that the form was named &quot;f1&quot; and the listbox was named &quot;list1&quot;, the script to clear the listbox would look like this:

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function clearcombo(){
for (var i=document.f1.list1.options.length-1; i>=0; i--){
document.f1.list1.options = null;
}
document.f1.list1.selectedIndex = -1;
}
</SCRIPT>

That is, one entry at a time, from the highest-numbered element in the list to the lowest-numbered element in the list, each element of the box is deleted by being set to null. The options[] array is zero based, the highest-numbered element can be found from elementname.options.length - 1. We traverse from the highest index to the lowest index because if we went from lowest to highest, each element we removed would change the indexes of the remaining elements. At the end of the loop, the &quot;selected&quot; element of the list is reset to &quot;-1&quot;, which is the JavaScript list/combo convention for &quot;nothing selected.&quot;

Suppose you wanted to fill the box back up?

[this is a filler entry] Click here to fill the box.

Here's the script that fills in the box...

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function fillcombo(){
for (var i = 0; i < 10; i++){
document.f1.list1.options[document.f1.list1.options.length]=
new Option(&quot;display string &quot; + i, &quot;value &quot; + i);
}
}
</SCRIPT>

The JavaScript &quot;new Option()&quot; command lets you give the contents of the listbox or combo box both a displayable text string (the first parm) and a &quot;value&quot; that gets transmitted back up to the server when the form is submitted (the second parm).

Putting It All Back Together

You can see where we're going with this, right?

At this point our listbox-populating loop looks like this... notice how we've added the &quot;ONCHANGE&quot; handler...

<%
Set rsX = Server.CreateObject(&quot;ADODB.Recordset&quot;)
sQuery = &quot;SELECT Name, ID FROM Manufacturers ORDER BY Name&quot;
rsX.Open sQuery, objConn, adOpenForwardOnly, _
adLockReadOnly, adCmdText
If rsX.EOF Then
Response.Write &quot;No manufacturers.<BR>&quot;
Else
Response.Write &quot;<SELECT NAME=&quot;&quot;manufacturer&quot;&quot; SIZE=15 &quot; & _
ONCHANGE=&quot;&quot;manuselected();&quot;&quot; >&quot;
Do Until rsX.EOF
Response.Write &quot;<OPTION VALUE=&quot;&quot;&quot; & rsX(&quot;ID&quot;) & _
&quot;&quot;&quot;>&quot; & rsX(&quot;Name&quot;) & &quot;</OPTION>&quot;
rsX.MoveNext
Loop
Response.Write &quot;</SELECT>&quot;
End If
rsX.Close
Set rsX = Nothing
%>

Well, first we have to change our SQL query to something that will pull up not just manufacturers, but car models as well. A comprehensive tutorial in SQL is beyond our current scope (though you could find some good info in this article from LearnASP.com), but in general the &quot;JOIN&quot; clause lets you combine values from multiple tables. In the case of our car models example, the following query would work

SELECT Manufacturers.Name, Cars.Model,
Manufacturers.ID AS ManuID, Cars.ID AS ModelID
FROM Cars INNER JOIN Manufacturers ON
Cars.Manufacturer = Manufacturers.ID
ORDER BY Manufacturers.Name, Cars.Model;

This query would pull up a recordset where all of the models from a manufacturer were listed together, and all of the manufacturers were listed in order. It would be something like shown in this table of our first few car models:

Manufacturer Manufacturer ID Model Model ID
Acura 105 3.2 TL 3024
Acura 105 3.2 RL 3027
Acura 105 Integra 3035
Audi 106 A4 Quattro 3046
Audi 106 A6 Quattro 3049
Audi 106 Fox 4067
BMW 108 318 4101
BMW 108 320 4055
BMW 108 M3 4097
BMW 108 Z3 4105

Notice that in our example we are working with the &quot;ID&quot; fields for both manufacturer and model, which (in our database) are numeric data types. If you are trying to duplicate this exercise against a database with non-numeric keys you will probably have to modify the script to enclose the &quot;ID&quot; fields in quotes.

The trick will be to loop through the recordset, filling in the first listbox with the names and IDs of each of the manufacturers, while at the same time filling in our client-side javascript with the commands that empty and fill in the second listbox. Are you ready?


<%
Set rsX = Server.CreateObject(&quot;ADODB.Recordset&quot;)
sQuery = &quot;SELECT Manufacturers.Name, Cars.Model, &quot; & _
&quot;Manufacturers.ID AS ManuID, Cars.ID AS ModelID &quot; & _
&quot;FROM Cars INNER JOIN Manufacturers &quot; & _
&quot;ON Cars.Manufacturer = Manufacturers.ID &quot; & _
&quot;ORDER BY Manufacturers.Name, Cars.Model&quot;
rsX.Open sQuery, objConn, adOpenForwardOnly, _
adLockReadOnly, adCmdText
If rsX.EOF Then
Response.Write &quot;No manufacturers.<BR>&quot;
Else
' write the manufacturers listbox...
Response.Write &quot;<SELECT NAME=&quot;&quot;manufacturer&quot;&quot; SIZE=15&quot; & _
&quot; ONCHANGE=&quot;&quot;manuselected(this);&quot;&quot; >&quot;
' write the entry code for the javascript...
sJavaScript = &quot;function manuselected(elem){&quot; & vbCrlf & _
&quot;for (var i = document.f1.model.&quot; & _
&quot;options.length; i >= 0; i--){&quot; & vbCrlf & _
&quot;document.f1.model.options = null;&quot; & _
vbCrlf
' loop through the recordset...
Do Until rsX.EOF
' is this a new manufacturer?
If sLastManufacturer <> rsX(&quot;Name&quot;) Then
' if so, add an entry to the first listbox
sLastManufacturer = rsX(&quot;Name&quot;)
Response.Write &quot;<OPTION VALUE=&quot; & rsX(&quot;ManuID&quot;) & _
&quot;>&quot; & sLastManufacturer & &quot;</OPTION>&quot;
' and add a new section to the javascript...
sJavaScript = sJavaScript & &quot;}&quot; & vbCrlf & _
&quot;if (elem.options[elem.selectedIndex].value==&quot; & _
rsX(&quot;ManuID&quot;) & &quot;){&quot; & vbCrlf
End If
' and add a new model line to the javascript...
sJavaScript = sJavaScript & _
&quot;document.f1.model.options[document.&quot; & _
&quot;f1.model.options.length] = new Option('&quot; & _
rsX(&quot;Model&quot;) & &quot;','&quot; & rsX(&quot;ModelID&quot;) & _
&quot;');&quot; & _
vbCrlf
rsX.MoveNext
Loop
' finish the manufacturer listbox...
Response.Write &quot;</SELECT>&quot;
' create the models listbox...
Response.Write &quot;<SELECT NAME=&quot;&quot;model&quot;&quot; SIZE=15>&quot;
Response.Write &quot;<OPTION>[none currently selected]</OPTION>&quot;
Response.Write &quot;</SELECT>&quot;
' put the last line on the javascript...
' and write it out...
sJavaScript = sJavaScript & vbCrlf & &quot;}&quot; & vbCrlf & _
&quot;}&quot; & vbCrlf
Response.Write &quot;<SCR&quot; & _
&quot;IPT LANGUAGE=&quot;&quot;JavaScript&quot;&quot;>&quot; & vbCrlf
Response.Write sJavaScript & vbCrlf & &quot;</SCR&quot; & _
&quot;IPT>&quot; & vbCrlf
End If
rsX.Close
Set rsX = Nothing
%>


Let me know how it turns out for you,
CatManDave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top