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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic List Boxes

Status
Not open for further replies.

MalCarne

Technical User
Apr 1, 2005
70
US
I've spent the better part of today googling dynamic select boxes and read a lot of ways to create them. However, none of the articles I've read address the lists being data driven.
So, here's what I've done and am trying to do. I start with a recordset and convert it into an array using .getrows
The array has 4 elements; ID, drug name, a delimited string of possible dosages, and a delimited string of possible quantities. The parent select is created like this:

Code:
<select name = drug onchange=fnDosage(this.options[options.selectedIndex].value,<%=arrDrug%>);>
       <option value=choose selected>..Choose..</option>
           <%  
             for i = 0 to ubound(arrDrug,2)
              drug_id = arrDrug(0,i)
              drug=arrDrug(1,i)
              if hist1 = drug_id then
                 selected = "Selected"
              end if
              response.Write("<option value="& i & selected & ">" & drug &"</option>")
               next  
              %>

What I'm trying to do is create 2 child selects; one for dosage, the other for quantity using the avalible values in the delimited strings in the array.
I have no idea if this is the right way to do this, but I want to keep form submission and database hits to a minimum.
Any suggestions?
 
Sheco said:
Your options, from easiest to hardest:

1. Use a page transion so that the user selects from first option list and submits form. The next page will then present a second list based on the option selected on the first page.

2. Use ASP to write all possibilites into client-side variables and then use client-side logic to populate the second list.

3. Use the client-side xmlhttp object to request data from an ASP that returns the second list of options in xml format... This is basically a remote procedure call. This technique is also known as AJAX.
 
I would suggest number 2. Output the javascrpt code for a dosage/quantity array thats indexes match the indexes of the select dropdown. Put an onChange in the select dropdown that calls a populate function with it's selected index. In the populate function, clear the child dropdowns, then loop through the portions of the javascript array for that index, creating a new option for each value and adding it to your second/third dropdown.

Here's how to build an array:
Code:
Response.Write "<script language=""javascript"">" & vbCrLf
Response.Write "var arrValues = new Array("
Dim rowCtr, lastVal
For rowCtr = 0 to UBound(myArr,2)
   'output a comma if we are on any record after the first one
   If rowCtr > 0 Then Response.Write ","

   'start two element array to hold arrays of dosages and amounts
   Response.Write "new Array("

   'output array of dosages - turn it into comma-delimited string with quotes around each value
   Response.Write "new Array('" & Replace(myArray(rowCtr,2),"','") & "'),"

   'output array of amounts
   Response.Write "new Array('" & Replace(myArray(rowCtr,3),"','") & "')"
   
   'end two element array
   Response.Write ")"
Next

'end outer array
Response.Write ");" & vbCrLf
Response.Write "</script>"
<%

Now, the array is technically 3 layers deep. The first layer should line up with the index for your dropdown, since it was filled from the same array. You will have to subtract 1 from the selected index to make it line up perfect since you have an extra option at the beginning of your dropdown. The second index is to choose either dosage [0] or amounts [1]. The third index is the dosage or amount indexes.

So if you wanted to loop through the array in javascript for a given index, it would be something like:
Code:
var myIndex = 5;
'loop through dosages
for(var i = 0; i < arrValues[myIndex][0].length;i++){
   alert("Dosage #" + i + " = " + arrValues[myIndex][0][i]);
}

'loop through amounts
for(var i = 0; i < arrValues[myIndex][1].length;i++){
   alert("Amount #" + i + " = " + arrValues[myIndex][1][i]);
}


Note: I have seen several of the ASP-driven cascading dropdown tutorials on the net. Mine (in the FAQs) is not all that great since it uses a fairly complex javascript array (I've been thinking about switching it), however everything else you need to know should be in there. Basically the only piece missing is to build a function to populate the two dropdowns based on the array values in the above code section.

-T

barcode_1.gif
 
And perhaps this may hopefully give you some ideas. It's got a downloadable sample and does retain values after the post back (although it is admittedly somewhat complicated):

Classic ASP Design Tips - Dependent Listboxes


Best regards,
-Paul
- Freelance Web and Database Developer
- Classic ASP Design Tips
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top