当前位置:编程学习 > asp >>

利用ASP嵌套JS+SQL Server打造两级连动下拉框(二)

答案: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环境下调试通过。

(全文完)

上一个:不用组件上载文件代码具体例子
下一个:利用ASP嵌套JS+SQL Server打造两级连动下拉框(一)

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,