Ver Mensaje Individual
  #12 (permalink)  
Antiguo 26/02/2009, 17:45
registros_r2
 
Fecha de Ingreso: febrero-2009
Mensajes: 23
Antigüedad: 15 años, 9 meses
Puntos: 0
Respuesta: 3 combobox dependientes

Hola a todos:

Que tal Adler, no he conseguido hacer funcionar tu codigo, no se porque!!!, pero tengo este otro que he encontrado y funciona muy bien, salvo que cuando hago la primera seleccion estan los resultados filtrados, pero en la segunda me salen resultados repetidos, no se si me entiendes... bueno, se puede solucionar, os dejo el codigo que le puede venir bien a hugorei
Código:
<%
' this section of code is self-contained and suitable to be moved to an include file
Function TripleLinkedList(oCon, sQuery, sFormFieldName, nSize, sDBField1, sDBField2, sDBField3, sDBFieldResult)
 ' this is a general-purpose routine that implements triple-linked
 ' listboxes. here is the drill
 Dim sTemp  ' general-purpose temp variable
 Dim sScript  ' bucket for holding the script structure
 Dim sSelect  ' bucket for the <SELECT> statement
 Dim sArray1  ' bucket to store the DBField2 array
 Dim nField1  ' counter for the primary array
 Dim sArray2  ' bucket to store the DBField3 array
 Dim nField2  ' counter for the secondary array
 Dim rs   ' recordset
 Dim sLastVal1 ' comparison string to test for record changes
 Dim sLastVal2 ' comparison string to test for record changes
 On Error Resume Next
 If Not IsObject(oCon) Then
  sScript = "error processing triplelist -- need a connection object."
 ElseIf oCon.State <> 1 Then
  If Err.number <> 0 Then
   sScript = "error processing triplelist -- invalid connection object."
  Else
   sScript = "error processing triplelist -- connection is not open."
  End If
 Else
  Set rs = oCon.Execute(sQuery)
  If Err.number <> 0 Then
   sScript = "error processing query. Error " & Hex(Err.number) & ": " & Err.Description
  ElseIf rs.EOF Then
   sScript = "no records found -- seems wrong"
  Else
   On Error Goto 0 
   sScript = "<SCR" & "IPT LANGUAGE=""JavaScript"">" & vbCrlf
   sScript = sScript & "var array1 = new Array();" & vbCrlf
   sScript = sScript & "var array2 = new Array();" & vbCrlf
   sSelect = "<SELECT NAME=""" & sFormFieldName & "1"" SIZE=""" & nSize & _
	""" ONCHANGE=""return(clickcombo(1,document.forms[0]." & _
	sFormFieldName & "1,document.forms[0]." & sFormFieldName & _
	"2,document.forms[0]." & sFormFieldName & "3));""><OPTION>Select a value</OPTION>" & vbCrlf
   sLastVal1 = "empty" ' set up a default test value...
	  Do Until rs.EOF
		  If rs(sDBField1) <> sLastVal1 Then
	 If Right(sArray1, 3) = "," & vbCrlf Then
	  ' pull off any trailing commas
	  sArray1 = Left(sArray1, Len(sArray1) - 3)
	 End If
	 If Len(sArray1) > 0 Then
	  sArray1 = sArray1 & ");" & vbCrlf
	 End If
	 ' pick up new information for this row...
	 nField1 = nField1 + 1
	 sLastVal1 = rs(sDBField1)
	 ' write the new contents of field 1 to the select statement
	 sSelect = sSelect & "<OPTION VALUE=" & nField1 & ">" & sLastVal1 & "</OPTION>"
	 ' write a new entry in array1 for the field 2 values...
	 sArray1 = sArray1 & "array1[" & nField1 & "] = new Array(" & vbCrlf
	 ' and reset the test values for field 2
	 nField2 = 0
	 sLastVal2 = "empty"
	End If
	If sLastVal2 <> rs(sDBField2) Then
	 If Right(sArray2, 3) = "," & vbCrlf Then
	  ' pull off any trailing commas
	  sArray2 = Left(sArray2, Len(sArray2) - 3)
	 End If
	 If Len(sArray2) > 0 Then
	  sArray2 = sArray2 & ");" & vbCrlf
	 End If
	 ' pick up new information for this row...
	 sLastVal2 = rs(sDBField2)
	 nField2 = nField2 + 1
	 ' write a new entry in array1 containing this set of field 2 values...
	 sArray1 = sArray1 & "	" & 1000 * nField1 + nField2 & ",""" & sLastVal2 & """," & vbCrlf
	 ' write a new entry in array2 for this set of field 3 values...
	 sArray2 = sArray2 & "// values for " & sLastVal2 & vbCrlf
	 sArray2 = sArray2 & "array2[" & 1000 * nField1 + nField2 & "] = new Array(" & vbCrlf
	End If
	' write the field3 values to the field2 array...
	sArray2 = sArray2 & "	" & rs(sDBFieldResult) & ",""" & rs(sDBField3) & """," & vbCrlf
	rs.MoveNext ' move on to the next record...
   Loop
   ' if these arrays weren't previously closed out, then close them now
   If Right(sArray2, 3) = "," & vbCrlf Then
	' pull off any trailing commas
	sArray2 = Left(sArray2, Len(sArray2) - 3)
   End If
   If Len(sArray2) > 0 Then
	sArray2 = sArray2 & ");" & vbCrlf
   End If
   ' if these arrays weren't previously closed out, then close them now
   If Right(sArray1, 3) = "," & vbCrlf Then
	' pull off any trailing commas
	sArray1 = Left(sArray1, Len(sArray1) - 3)
   End If

   If Len(sArray1) > 0 Then
	sArray1 = sArray1 & ");" & vbCrlf
   End If
   ' close out the listbox/combobox and add the second and third listbox/combobox entries...
   sSelect = sSelect & "</SELECT>" & vbCrlf
   sSelect = sSelect & "<SELECT NAME=""" & sFormFieldName & "2"" SIZE=""" & nSize & _
	""" ONCHANGE=""return(clickcombo(2,document.forms[0]." & sFormFieldName & _
	"1,document.forms[0]." & sFormFieldName & "2,document.forms[0]." & _
	sFormFieldName & "3));""><OPTION>Select a value</OPTION></SELECT>" & vbCrlf
   sSelect = sSelect & "<SELECT NAME=""" & sFormFieldName & "3"" SIZE=""" & nSize & _
	"""><OPTION>Select a value</OPTION></SELECT>" & vbCrlf
   ' finally clean up the script and write the whole thing out as a block
   sScript = sSelect & vbCrlf & _
	sScript & vbCrlf & _
	sArray1 & vbCrlf & _
	sArray2 & vbCrlf & _
	"</SCR" & "IPT>" & vbCrlf
  ' -----------------------------------------------------------------
  End If
  ' close and free the recordset
  rs.Close
  Set rs = Nothing
 End If
 ' and get the fleep outta here
 TripleLinkedList = sScript
End Function
%>

<SCRIPT LANGUAGE="JavaScript">
<!-- 
function clearcombo(elem){
 var i;
 for (i = elem.options.length; i >= 0; i--) elem.options[i] = null;
 elem.selectedIndex = -1;
}
function populatecombo2(elem, index){
 if (array1.length >= index){
  if (array1[index]){
   for (var i = 0; i < array1[index].length; i= i + 2){
	elem.options[elem.options.length] = new Option(array1[index][i + 1], array1[index][i]);
   }
  }
  else{
   elem.options[elem.options.length] = new Option("[none available]", 0);
  }
 }
 else{
  elem.options[elem.options.length] = new Option("[none available]", 0);
 }
}
function populatecombo3(elem, index){
 if (array2.length >= index){
  if (array2[index]){
   for (var i = 0; i < array2[index].length; i= i + 2){
	elem.options[elem.options.length] = new Option(array2[index][i + 1], array2[index][i]);
   }
  }
  else{
   elem.options[elem.options.length] = new Option("[none available]", 0);
  }
 }
 else{
  elem.options[elem.options.length] = new Option("[none available]", 0);
 }
}
function clickcombo(nWhich,elem1,elem2,elem3){
 if (nWhich == 1){
  clearcombo(elem2);
  clearcombo(elem3);
  populatecombo2(elem2, elem1[elem1.selectedIndex].value);
 }
 if (nWhich == 2){
  clearcombo(elem3);
  populatecombo3(elem3, elem2[elem2.selectedIndex].value);
 }
 return true;
}
// -->
</script>


<% 
Dim Conn
Dim sQuery
Set Conn = Server.CreateObject("ADODB.Connection")
'conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\websites\gotomygymcom\app\classes.mdb;Persist Security Info=False"
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.mappath("classes.mdb")
conn.Open ' connect to the database
' -----------------------------------------------
' then set up a query
' -----------------------------------------------
sQuery = "SELECT distinct * from classlist"
If (Len(request.form("class_level")) > 0) Or (Len(request.form("weekday")) > 0) Then
   sQuery = sQuery & "Where "
   If Len(request.form("class_level")) > 0 Then
      sQuery = sQuery & "class_level = " & request.form("class_level")
   End If
   If Len(request.form("weekday")) > 0 Then
      sQuery = sQuery & " And weekday = " & request.form("weekday")
   End If
End If

sQuery = sQuery & " order by start_age"
%>


<%response.write(class_level)%>

<FORM id=form1 name=form1 ACTION="BBBBBBB.asp" METHOD="post">
<CENTER>
<%
Response.Write TripleLinkedList(Conn, sQuery, "listbox", 1, "class_level", "weekday", "start_time", "class_id")
Conn.Close
Set Conn = Nothing
%>
<P><input type="Submit" value="ENTRAR">
</FORM>
Código:
BBBBBBB.asp

<%
Dim Lista
Lista = Request.Form("listbox1")
Response.Write Lista
%>

<%
Dim Listb
Listb = Request.Form("listbox2")
Response.Write Listb
%>
Espero que os sirva y me podais ayudar en el error que os comento