当前位置 > 文字教程 > asp教程

建立动态下拉式选单(三阶层)

文章类别:asp | 发表日期:2008-10-5 20:40:43

'建立動態下拉式選單 (三階層)

<%'将资料库连结字串, SQL 叙述,形成第一个表单字串变数 (colorlist),第一个表单名称 'color,形成第二个表单字串变数 (fruitlist),第二一个表单名称 fruit,形成第三个表单字串变数 '(pricelist),第三个表单名称 price,等资讯透过呼叫副程式的方式经过处理之后再传回主程式'中,请注意有某几个参数是以 ByRef 的方式传回主程式,而某些参数是以 ByVal 的方式传回'的紫色粗体字代表是 ByRef。请直接观看底下副程式的部分
myDSN="Provider=SQLOLEDB; Data Source=Jackal; Initial Catalog=pubs; User ID=sa; Password="
strSQL="select color.color, fruit.fruit, price.cost from color join fruit on color.ID=fruit.COLORID join price on price.FRUITID=fruit.ID order by color.ID"
call listmaker(myDSN, strSQL, colorlist,"color", fruitlist ,"fruit", pricelist, "price", pubevent, pubfun, "myForm")%><form name="myForm" method=post action=listbox2.asp>
颜色:
<%= colorlist%>

<BR>
水果:


<%= fruitlist%>

<BR>
价格:
<%= pricelist%>
<input type=submit value="送出">
</Form>
<SCRIPT Language="JavaScript"><!--

<%=pubevent%>
<%=pubfun%>
--></Script>


<%

SUB listmaker(myDSN, strSQL,byref list1, listname1, byref list2, listname2, byref list3, listname3, byref myevent, byref myfun, myForm)
Set my_conn = Server.CreateObject("ADODB.Connection")
my_conn.open myDSN
set rs=my_conn.execute(strSQL)
'利用 colorlist 字串变数来建立第一个下拉式选单的 Html 原始码。
colorlist="<select name=" & listname1
colorlist=colorlist & " OnChange=""Buildkey"
colorlist=colorlist & "(this.selectedIndex);"">"
'利用 fruitlist 字串变数来建立第二个下拉式选单的 Html 原始码。
fruitlist="<select name=" & listname2
fruitlist=fruitlist & " OnChange=""Buildkey1"
fruitlist=fruitlist & "(this.selectedIndex);"">"
'利用 pricelist 字串变数来建立第三个下拉式选单的 Html 原始码。
pricelist="<select name=" & listname3 & ">"
'在这里初始某些变数值,当作底下执行运算时之旗标值。
loopcounter=0
lastvalue=rs(0)
lastvalue2=rs(1)
redim tempArray(1)
'thisgroupcount 代表某种颜色共有几种水果
thisgroupcount=0
'thissubgroupcount 代表某种水果共有几种价格,当水果变换时 thissubgroupcount  会归零,并重新'累加。
thissubgroupcount=0
'howmanygroups 代表共有几种颜色
howmanygroups=0
'howmanysubgroups 代表共有几种水果,当颜色变换时 howmanysubgroups 会归零,并重新'累加。
howmanysubgroups=0
DO UNTIL rs.eof
thisvalue=rs(0)
thisvalue2=rs(1)
thisvalue3=rs(2)

'如果发现水果名称改变,而且目前水果是某种颜色的第一种水果时,譬如:红色←苹果 '或:黄色←柳丁时就在 Script 中加上颜色标记 (为了让大家更容易将 Script 区隔开'来),然后再将水果名称定义到 key 阵列值,
'接着再定义 key1[第几种颜色][第几种水果] 之阵列长度。

if thisvalue2<>lastvalue2 then
  If howmanysubgroups=0 then
  tempSTR1=tempSTR1 & "// " & lastvalue & vbcrlf
  tempSTR2=tempSTR2 & "// " & lastvalue & vbcrlf
  end If
  tempSTR2=tempSTR2 & "key1[" & howmanygroups & "][" & howmanysubgroups &
  "]=new Array(" & thissubgroupcount & ");" & vbcrlf
  tempSTR1=tempSTR1 & "key[" & howmanygroups & "][" & howmanysubgroups &
  "]=""" & lastvalue2 & """;" & vbCRLF
  howmanysubgroups=howmanysubgroups+1
  thissubgroupcount=0
  lastvalue2=thisvalue2
end if
'执行结果如下
// 红色
key[0][0]="苹果";
key[0][1]="莲雾";
key[0][2]="李子";
// 黄色
key[1][0]="柳丁";
key[1][1]="葡萄柚";
// 绿色
key[2][0]="芭乐";
key[2][1]="西瓜";
key[2][2]="枣子";
// 红色
key1[0][0]=new Array(3);
key1[0][1]=new Array(3);
key1[0][2]=new Array(3);
// 黄色
key1[1][0]=new Array(3);
key1[1][1]=new Array(3);
// 绿色
key1[2][0]=new Array(3);
key1[2][1]=new Array(3);
key1[2][2]=new Array(3);

'如果发现颜色名称改变,就定义 key[第几种颜色] 阵列值,
'接着再定义 key1[第几种颜色] 之阵列长度,重复定义的原因在之前已跟大家说过了,然'后将howmanygroups (颜色种类) 值加一,并将水果种类计数值 (thisgroupcount) 归零if thisvalue<>lastvalue then
tempSTR=tempSTR & "key[" & howmanygroups & "]=new Array(" & howmanysubgroups & ");" & _vbcrlf
tempSTR=tempSTR & "key1[" & howmanygroups & "]=new Array(" & howmanysubgroups & ");" & _vbcrlf
thisgroupcount=0
howmanygroups=howmanygroups+1
howmanysubgroups=0
end if

'执行结果如下
key=new Array(3);
key1=new Array(3);
key[0]=new Array(3);
key1[0]=new Array(3);
key[1]=new Array(2);
key1[1]=new Array(2);
key[2]=new Array(3);
key1[2]=new Array(3)
'如果 thisgroupcount=0 代表目前颜色改变,此时将颜色名称写到 color 下拉式选单中if thisgroupcount=0 then
colorlist =colorlist & "<option>" & thisvalue & "</option>" & vbcrlf
end if
'如果 thissubgroupcount=0 代表目前水果改变,此时在key1[第几种颜色][第几种水果][第几'种价格] 阵列值前加上水果名称标记,让大家更容易将水果名称与价格区隔开来。
if thissubgroupcount=0 then
tempSTR3=tempSTR3 & "// " & lastvalue2 & vbcrlf
end if
'定义key1[第几种颜色][第几种水果][第几种价格] 阵列值。
tempSTR3=TempSTR3 & "key1[" & howmanygroups & "][" & howmanysubgroups & "][" & thissubgroupcount & "]=""" & thisvalue3 & """;" & vbCRLF '执行结果:
// 频果
key1[0][0][0]="10元";
key1[0][0][1]="20元";
key1[0][0][2]="30元";
// 莲雾
key1[0][1][0]="40元";
key1[0][1][1]="50元";
key1[0][1][2]="60元";
// 李子
key1[0][2][0]="70元";
key1[0][2][1]="80元";
key1[0][2][2]="90元";
// 柳丁
key1[1][0][0]="100元";
key1[1][0][1]="110元";
key1[1][0][2]="120元";
// 葡萄柚
key1[1][1][0]="130元";
key1[1][1][1]="140元";
key1[1][1][2]="150元";
// 芭乐
key1[2][0][0]="160元";
key1[2][0][1]="170元";
key1[2][0][2]="180元";
// 西瓜
key1[2][1][0]="190元";
key1[2][1][1]="200元";
key1[2][1][2]="210元";
// 枣子
key1[2][2][0]="220元";
key1[2][2][1]="230元";
key1[2][2][2]="240元";

'如果目前是第一种颜色,而且水果名称正在改变,就将水果名称写到 fruit 下拉式选单中 if howmanygroups=0 then
If thissubgroupcount=0 then
fruitlist = fruitlist & "<option>" & thisvalue2 & "</option>" & vbcrlf
end if
End If

'如果目前是第一种颜色,而且是第一种水果,就将价格写到 price 下拉式选单。
if howmanygroups=0 then
if howmanysubgroups=0 then
pricelist = pricelist & "<option>" & thisvalue3 & "</option>" & vbcrlf
end if
End if

thisgroupcount=thisgroupcount+1
thissubgroupcount=thissubgroupcount+1
lastvalue=thisvalue
loopcounter=loopcounter+1
rs.movenext

LOOP
'别忘了最后一个 key[第几种颜色] 和 key1[第几种颜色] 的阵列长度还未定义。
tempSTR=tempSTR & "key[" & howmanygroups & _
"]=new Array(" & howmanysubgroups+1 & ");" & _
vbcrlf
tempSTR=tempSTR & "key1[" & howmanygroups & _
"]=new Array(" & howmanysubgroups+1 & ");" & _
vbcrlf

'别忘了最后一个 key[第几种颜色][第几种水果] 和 key1[第几种颜色][第几种水果] 的阵列长'度还未定义。
tempSTR2=TempSTR2 &"key1[" & howmanygroups & "][" & howmanysubgroups & "]=new Array(" & thissubgroupcount & ");" & _vbcrlf
tempSTR1= TempSTR1 & "key[" & howmanygroups & "][" & howmanysubgroups & "]=""" & lastvalue2 & """;" & vbCRLF tempSTR=tempSTR & TempSTR1 & TempSTR2 & tempSTR3
'将 color 下拉式选单作一个结尾
colorlist=colorlist & "</select>"
'将 fruit 下拉式选单作一个结尾
fruitlist= fruitlist & "</select>"
'将 price 下拉式选单作一个结尾
pricelist= pricelist & "</select>"

'一开始的 key 和 key1 阵列也不要忘记定义了。

myevent=vbcrlf & "key=new Array(" & howmanygroups+1 & ");"
myevent=myevent & vbcrlf & "key1=new Array(" & howmanygroups+1
myevent=myevent & ");" & vbcrlf & tempSTR

'大功告成,可以关闭资料库了。
rs.close
set rs=nothing
my_conn.close
set my_conn=nothing

'先产生 Buildkey 副程式,这边一点都不困难,原理在上面已经解释过了,笔者不再赘述,执行结果为:
function Buildkey(num)
{
Buildkey1(0);
document.myForm.fruit.selectedIndex=0;
for(ctr=0;ctr<key[num].length;ctr++)
{
document.myForm.fruit.options[ctr]=new Option(key[num][ctr],key[num][ctr]);
}
document.myForm.fruit.length=key[num].length;
}

tempSTR =vbcrlf & "function Buildkey" & "(num)" & vbcrlf
tempSTR =tempSTR & "{" & vbcrlf
tempSTR =tempSTR & "Buildkey1(0);" & vbcrlf
tempSTR =tempSTR & "document." & myForm & "."
tempSTR =tempSTR & listname2 & ".selectedIndex=0;" & vbcrlf
tempSTR =tempSTR & "for(ctr=0;ctr<key[num].length;ctr++)" & vbcrlf
tempSTR =tempSTR & "{" & vbcrlf
tempSTR =tempSTR & "document." & myform & "." & listname2
tempSTR =tempSTR & ".options[ctr]=new Option(key[num][ctr],"
tempSTR =tempSTR & "key[num][ctr]);" & vbcrlf
tempSTR =tempSTR & "}" & vbcrlf
tempSTR =tempSTR & "document." & myForm & "." & listname2
tempSTR =tempSTR & ".length=key[num].length;" & vbcrlf
tempSTR =tempSTR & "}" & vbcrlf

'再产生 Buildkey1 副程式
function Buildkey1(num)
{
document.myForm.price.selectedIndex=0;
for(ctr=0;ctr<key1[document.myForm.color.selectedIndex][num].length;ctr++){
document.myForm.price.options[ctr]=new Option(key1[document.myForm.color.selectedIndex][num][ctr],key1[document.myForm.color.selectedIndex][num][ctr]);
}
document.myForm.price.length=key1[document.myForm.color.selectedIndex][num].length;
}

tempSTR =tempSTR & vbcrlf & "function Buildkey1" & "(num)" & vbcrlf
tempSTR =tempSTR & "{" & vbcrlf
tempSTR =tempSTR & "document." & myForm & "."
tempSTR =tempSTR & listname3 & ".selectedIndex=0;" & vbcrlf
tempSTR =tempSTR & "for(ctr=0;ctr<key1[document.myForm.color.selectedIndex][num].length;ctr++)" & vbcrlftempSTR =tempSTR & "{" & vbcrlf
tempSTR =tempSTR & "document." & myform & "." & listname3
tempSTR =tempSTR & ".options[ctr]=new Option(key1[document.myForm.color.selectedIndex][num][ctr],"
tempSTR =tempSTR & "key1[document.myForm.color.selectedIndex][num][ctr]);" & vbcrlftempSTR =tempSTR & "}" & vbcrlf
tempSTR =tempSTR & "document." & myForm & "." & listname3
tempSTR =tempSTR & ".length=key1[document.myForm.color.selectedIndex][num].length;" & vbcrlftempSTR =tempSTR & "}" & vbcrlf
myfun=tempSTR
END sub
%>