日期:2011-02-12  浏览次数:21066 次

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

<%'将资料库连结字串, 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(" & how