This topic is locked

Linking Lookup Wizard dropdown

1/18/2005 11:37:03 AM
ASPRunnerPro General questions
T
troyo author

There were a few post that dealt with having multiple lookup wizard dropdown menus where the choice of dropdown A determined what was listed in B and so on. I was wondering if anyone had been able to get this to work with ASPRunner and if so, how they did it. As always, any help is greatly appreciated.
Troy

Sergey Kornilov admin 1/24/2005

Troy,
here is the sample code I use for this purpose. Probably you will be able to modify it for your task. This example builds a relationship between Make and Model dropdown boxes. You need to place it to ADD/EDIT pages somewhere close to the beginning of the file.
Here are the tables involved:
tblMake (ID, Name)

tblModel (MakeID, ID, Name)
tblMake.ID = tblModel.MakeID

' open database connection

Set rs = server.CreateObject ("ADODB.Recordset")

set dbConnection = server.CreateObject ("ADODB.Connection")

dbConnection.ConnectionString = strConnection

dbConnection.Open

Call ReportError
Set rs1 = server.CreateObject ("ADODB.Recordset")

Set rs2 = server.CreateObject ("ADODB.Recordset")

Set rs3 = server.CreateObject ("ADODB.Recordset")

Set rs4 = server.CreateObject ("ADODB.Recordset")

rs1.Open "select count()+1 from tblMake", dbConnection
%> <script language="JavaScript">
arr=new Array(<%=rs1(0)%>);

arr[0]=new Array(1);
<%
rs1.close

set rs1=nothing
rs2.Open "select distinct ID, Name from tblMake", dbConnection

i=0

while not rs2.eof

i=i+1

rs4.Open "select count(
)+1 from tblModel where MakeID=" & rs2(0), dbConnection

response.write "arr[" & i & "]=new Array(" & rs4(0) & ");" & vbcrlf

rs4.close

rs2.movenext

wend
i=0

j=0
rs2.close
%>

arr[0][0]="Please select"

<%
rs3.Open "select MakeID, CStr(ID)+'|'+Name from tblModel order by 1", dbConnection
val1= ""

while not rs3.eof

if val1<>rs3(0) then

 i=i+1

 val1=rs3(0)

 response.write "arr[" & i & "][0]=""Please select""" & vbcrlf

 j=0

end if

j=j+1

response.write "arr[" & i & "][" & j & "]=""" & rs3(1) & """" & vbcrlf

rs3.movenext

wend

rs3.close

%>
function BuildSecondDropDown(num)

{
document.forms.editform.Model.selectedIndex=0;
for(ctr=0;ctr<arr[num].length;ctr++)

{

 var a = arr[num][ctr].split('|');

 document.forms.editform.Model.options[ctr]=new Option(a[1],a[0]);

}
document.forms.editform.Model.length=arr[num].length;

}
function SetSelection(first, second)

{

var ctr;

   for (ctr=0; ctr<document.forms.editform.Make.length; ctr++)

 if (document.forms.editform.Make.options[ctr].value.toLowerCase() == first.toLowerCase() )

  {

  document.forms.editform.Make.selectedIndex = ctr;

  break;

   }

BuildSecondDropDown(document.forms.editform.Make.selectedIndex);Â

for (ctr=0; ctr<document.forms.editform.Model.length; ctr++)

 if (document.forms.editform.Model.options[ctr].value.toLowerCase() == second.toLowerCase() )

  {

  document.forms.editform.Model.selectedIndex = ctr;

  break;

   }
}

</script>


Also you need to modify Function BuildSelectControl include/..._aspfunction.asp file to call BuildSecondDropDown function when first drop-down box selection changes. See my changes in bold:

if strName = "Make" then

 BuildSelectControl = BuildSelectControl & "<select "& " onchange=""BuildSecondDropDown(this.selectedIndex);"" " &"size = " & strSize & " name=""" & strName & """ >"

else   Â

BuildSelectControl = BuildSelectControl & "<select size = " & strSize & " name=""" & strName & """>"

end if

D
dheydt 3/3/2005

Sergey,
I have tried this and it works great on the "Add" screen, however on the "Edit" screen it doesn't work properly. I was able to solve part of the problem by placing

onload="BuildSecondDropDown(document.forms.editform.work_order.selectedIndex);"

into the <BODY> tag.
However I can't seem to get the initial screen to display the current value for the second select. For example, If I add a record with work_order = "A100" and contract = "C4", then I later try to edit this record, it will display work_order = "A100" but contract will say "Please Select...". It should have already selected "C4".
Thanks for your help.
Dan