Aqui el codigo para 2,3,4.. combos enlazados
Aqui les paso el codigo para que puedan crear sus combos enlazados de una manera facil y sencilla, se pueden hacer desde 2 hasta n combos enlazados!!!
Esta pagina se llama combo.asp
<% response.buffer = true %>
<Script language="VBScript" RUNAT="SERVER">
FUNCTION Genera_OptionCollection(oCollection,CSqlString)
' Esta funcion genera un arreglo con los que recupera de la base de datos para el 2 y 3er combo
Comilla = Chr(34)
SET oHandle=Server.CreateObject("ADODB.Connection")
oHandle.Open "Oracle8"
SET oRegistro = oHandle.Execute(CSqlString)
Genera_OptionCollection = "<scri" & "pt language='JavaScript'>" & vbCrlf
Genera_OptionCollection = Genera_OptionCollection &_
"var " & oCollection & " = new OptionCollection();" & vbCrlf
oRegistro.MoveFirst
DO UNTIL oRegistro.Eof
' Ejemplo: oSubFuncion.Add("0101","Descripción del Catalogo","01");
Genera_OptionCollection = Genera_OptionCollection &_
oCollection & ".Add(" & Comilla & oRegistro("Child")& Comilla & "," &_
Comilla & oRegistro("ChildCaption")& Comilla & "," &_
Comilla & oRegistro("Parent")& Comilla & ");" & vbCrlf
oRegistro.MoveNext
LOOP
oRegistro.Close
oHandle.Close
Genera_OptionCollection = Genera_OptionCollection & "</scr" & "ipt>" & vbCrlf
END FUNCTION
</script>
<HTML>
<HEAD>
<TITLE>Combos</TITLE>
<!--INCLUIR ESTE ARCHIVO, ES EL QUE HACE TODO!!! -->
<script language=Javascript src="optionex.js"></script>
</HEAD>
<BODY>
<%
' -------------------------- Aqui armo la cadena que llena el segundo combo -----------------------------
CSqlString = "SELECT CSCClave||CSCCapitu Child,CSCDescri ChildCaption,CSCCapitu Parent " &_
"FROM SATCSubCapit " &_
"ORDER BY Parent,Child,ChildCaption"
Response.Write Genera_OptionCollection("oSubCapit",CSqlString)
' -------------------------- Aqui armo la cadena que llena el tercer combo -----------------------------
CSqlString = "SELECT CPAClave||CPaSubCap||CPaCapitu Child,CPaDescri ChildCaption,CPaSubCap||CPaCapitu Parent " &_
"FROM SATCPartida " &_
"ORDER BY Parent,Child,ChildCaption"
Response.Write Genera_OptionCollection("oPartida",CSqlString)
%>
<form name=forma>
<table>
<tr>
<td align=right valign=top>Capitulo </td>
<td align=left valign=top>
<SELECT name="PCapitulo" onchange="UpdateSelect('SubCap','forma',this.optio ns[this.selectedIndex].value,oSubCapit);" style="width:400px;">
<%' Aqui construimos La Lista del primer combo
SET oHandle=Server.CreateObject("ADODB.Connection")
oHandle.Open "Oracle8"
CSqlString = "SELECT CCaClave,SUBSTR(CCaDescri,1,50) CCaDescri " &_
"FROM SaTCCapitulo " &_
"ORDER BY CCaClave"
SET oRegistroCa = oHandle.Execute(CSqlString)
oRegistroCa.MoveFirst
Do UNTIL oRegistroCa.EOF %>
<OPTION Value="<%=oRegistroCa("CCaClave")%>">
<%=oRegistroCa("CCaDescri")%> </option>
<% oRegistroCa.MoveNext
LOOP
oRegistroCa.close
%>
</SELECT> </td>
</tr>
<tr>
<td align=right valign=top>Subcapitulo</td>
<td align=left valign=top><SELECT name="SubCap" onchange="UpdateSelect('Partida','forma',this.opti ons[this.selectedIndex].value,oPartida);" style="width:400px;">
<option>[Seleccione un valor]</option>
</select></td>
</tr>
<tr>
<td align=right valign=top><font size="2" face="Arial, Helvetica, sans-serif"><strong>Partida</strong></font>:</td>
<td align=left valign=top><SELECT name="Partida" style="width:400px;">
<option>[Seleccione un valor]</option>
</select></td>
</tr>
</table>
</form>
</BODY>
</HTML>
Aqui pongo el archivo que se llama optionex.js, a este no le tienen que modificar NADA
var NS4 = (document.layers)?true:false;
var IE4 = (document.all)?true:false;
function _struct(child,childcaption,parent){
this.child = child;
this.childcaption = childcaption;
this.parent = parent;
}
function _addobject(child,childcaption,parent){
var obj = new _struct();
obj.child = child;
obj.childcaption = childcaption;
obj.parent = parent;
return(obj);
}
function insertEntry(child,childcaption,parent){
if(child.length > 0 ){
for(var i=0;i<this.length;i++)
if(this.items[i].child == child)
return;
this.items[this.length++] = _addobject(child,childcaption,parent);
}
}
function OptionCollection(){
this.length=0;
this.items = new Object();
this.Add=insertEntry
}
function clearSelect(oSelect){
var iCount=0;
if(!oSelect) return;
iCount = oSelect.options.length;
for(var i=0;i<iCount;i++){
if(IE4)
oSelect.options.remove(0);
else if(NS4)
oSelect.options[0]=null;
}
}
function agregaOpcion(oSelect,nIndice,cClave,cDescri){
elSelectOption = new Option();
elSelectOption.value = cClave;
elSelectOption.text = cDescri;
if(NS4) oSelect.options[nIndice] = elSelectOption;
else if(IE4) oSelect.options.add(elSelectOption,nIndice);
delete(elSelectOption);
}
function addOptions(oSelect,olist,parent){
var elOption;
var idxSelect=0;
//check arguments.
if((!olist) || (!oSelect))return;
if(parent.length <= 0) return;
//now looop through all the list items only entering relevant options.
clearSelect(oSelect);
//add a blank list
// - elBlankOption = new Option();
// - elBlankOption.text = "";
// - if(NS4) oSelect.options[0] = elBlankOption
// - else if(IE4) oSelect.options.add(elBlankOption,0)
// - elBlankOption.value = "";
for(var i=0;i<olist.length;i++){
//only the child entries of the parent
if(olist.items[i].parent == parent){
if(idxSelect==0){
agregaOpcion(oSelect,0,"","Seleccione un valor de la lista");
idxSelect++;
}
elOption = new Option();
//now IE and NS have a different way to handle object creation.
if(NS4){
elOption.value = olist.items[i].child
elOption.text = olist.items[i].childcaption
oSelect.options[idxSelect] = elOption
}else if(IE4){
elOption.value = olist.items[i].child
elOption.text = olist.items[i].childcaption
oSelect.options.add(elOption,idxSelect)
}
idxSelect++;
delete(elOption);
}
}
if(oSelect.length<=0){
agregaOpcion(oSelect,0,"","No se encontraron valores coincidentes");
}
oSelect.selectedIndex = 0;
}
function clearOptions(szSelect,szForm){
if(szSelect.length <= 0 || szForm.length <=0)
alert("error:clearOption must pass arguments");
if(IE4)
clearSelect(document.all.item(szSelect));
else if(NS4)
clearSelect(eval('document.' + szForm + '.' + szSelect));
}
function listAllOptions(szSelect,szForm,olist){
var elOption;
var idxSelect=0;
var oSelect = new Object();
//check arguments.
if(!olist)return;
if(IE4)
oSelect = document.all.item(szSelect);
else if(NS4)
oSelect = eval('document.' + szForm + '.' + szSelect);
if(!oSelect)return;
//now looop through all the list items entering all
clearSelect(oSelect);
for(var i=0;i<olist.length;i++){
elOption = new Option();
//now IE and NS have a different way to handle object creation.
if(NS4){
elOption.value = olist.items[i].child
elOption.text = olist.items[i].childcaption
oSelect.options[idxSelect] = elOption
}else if(IE4){
elOption.value = olist.items[i].child
elOption.text = olist.items[i].childcaption
oSelect.options.add(elOption,idxSelect)
}
idxSelect++;
delete(elOption);
}
}
function UpdateSelect(szSelect,szForm,szParent,olist){
if(IE4)
addOptions(document.all.item(szSelect),olist,szPar ent);
else if(NS4)
addOptions(eval('document.' + szForm + '.' + szSelect),olist,szParent);
}