twolevel.asp
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<meta name="generator" content="microsoft frontpage 4.0">
<meta name="progid" content="frontpage.editor.document">
<title>下拉列表示例</title>
</head>
<%
dim conn
dim connstr
on error resume next
set conn=server.createobject("adodb.connection")
connstr="driver={sql server};server=wen;uid=sa;pwd=;database=db_provincecat"
conn.open connstr
set rs=server.createobject("adodb.recordset")
rs.cursorlocation = aduseclient
sql="select * from tb_province"
rs.open sql,conn,1,1
if not rs.eof then
dim strjscript
strjscript=""
response.write("<script language=javascript>")&chr(13)
response.write("function setcategory(s){")&chr(13)
response.write("s.category.length="+cstr(rs.recordcount+1)+";")&chr(13)
dim i
i=0
while not rs.eof
strjscript=strjscript+"s.category.options["+cstr(i)+"].value="+chr(34)+trim(rs("provincecode"))+chr(34)+";"+chr(13)
strjscript=strjscript+"s.category.options["+cstr(i)+"].text="+chr(34)+trim(rs("provincename"))+chr(34)+";"+chr(13)
i=i+1
rs.movenext
wend
response.write(strjscript)
response.write("s.category.options["+cstr(i)+"].value="+chr(34)+"无"+chr(34)+";"+chr(13))
response.write("s.category.options["+cstr(i)+"].text="+chr(34)+"**请选择省份**"+chr(34)+";"+chr(13))
response.write("s.category.options["+cstr(i)+"].selected=true;"+chr(13))
response.write("}")&chr(13)
rs.close
set rs=nothing
end if
set rs=server.createobject("adodb.recordset")
rs.cursorlocation = aduseclient
sql="select num=count(*),tb_province.provincecode from tb_province,tb_city where tb_province.provincecode=substring(tb_city.citycode,1,2) group by tb_province.provincecode order by tb_province.provincecode"
rs.open sql,conn,1,1
if not rs.eof then
dim num(30),j
j=0
do while not rs.eof
num(j)=rs("num")
j=j+1
rs.movenext
loop
end if
rs.close
set rs=nothing
sql=""
set rs=server.createobject("adodb.recordset")
rs.cursorlocation = aduseclient
sql="select tb_province.*,tb_city.* from tb_province,tb_city where tb_province.provincecode=substring(tb_city.citycode,1,2) order by tb_province.provincecode"
rs.open sql,conn,1,1
j=0
if not rs.eof then
response.write("function setsmall_cat(d){")&chr(13)
response.write("var valuecategory=d.category.options[d.category.selectedindex].value;")&chr(13)
response.write("if(valuecategory.indexof("+chr(34)+"无"+chr(34)+")==0){"+chr(13)&_
"d.small_cat.length=1;"&_
"d.small_cat.options[0].value="+chr(34)+"无"+chr(34)+";"+chr(13)&_
"d.small_cat.options[0].text="+chr(34)+"**请选择城市**"+chr(34)+chr(13)&_
"d.small_cat.options[0].selected=true;"+chr(13)&_
"}")
i=0
dim nextlevel,upcatcode,overflow
nextlevel="n"
overflow="n"
strjscript=""
do while not rs.eof
upcatecode=trim(rs("provincecode"))
if nextlevel="n" then
strjscript="else if(valuecategory.indexof("+chr(34)+upcatecode+chr(34)+")==0){"+chr(13)
strjscript=strjscript+"d.small_cat.length="+cstr(num(j))+";"+chr(13)
j=j+1
nextlevel="y"
end if
strjscript=strjscript+"d.small_cat.options["+cstr(i)+"].value="+chr(34)+trim(rs("citycode"))+chr(34)+";"+chr(13)
strjscript=strjscript+"d.small_cat.options["+cstr(i)+"].text="+chr(34)+trim(rs("cityname"))+chr(34)+";"+chr(13)
i=i+1
rs.movenext
if upcatecode<>trim(rs("provincecode")) then
overflow="y"
end if
if overflow="y" then
response.write(strjscript+"}")
strjscript=""
overflow="n"
nextlevel="n"
i=0
end if
loop
response.write("}</script>")&chr(13)
rs.close
set rs=nothing
end if
conn.close
set conn=nothing
%>
<body onload=setcategory(document.select);setsmall_cat(document.select)>
<form name="select" method="post" action="#">
<center>
<table>
<tr> <td>请选择:</td>
<td><select onchange=setsmall_cat(document.select) name=category size="1" ></select> <select name=small_cat size="1" ></select> </td>
</tr>
</table></center>
</form>
</body>
</html>
第三,进行代码调试。
本程序在win2000+iis+sql server7.0环境下调试通过。
(全文完)
