開始在網上查找到asp的無限分類,但是asp.net的幾乎找不到,找到的也是跟 TreeView結合起來的.
找到asp版本的代碼有幾種,原來都差不多是采用遞 歸算法.
其中表結構都是如下:
表名稱是ClassName
id 主鍵
sid 對應的父類的 id
ClassName 對應類別的名稱.
代碼段 一:
1function loadNextType(upid,rank)
2 dim rs
3 set rs="select * from classname where sid="&upid
4 do while not rs.eof
5 loadNextType=loadNextType &rs("ClassName") &"<br>"& string("-",rank) & loadNextType(rs("id"),rank+1)
6 rs.movenext
7 loop
8end function調用時:response.write(loadNextType (0,0))
另外一段代碼就跟上面原理就一樣的.只不過是加入了樹 型結構的顯示方式.
代碼段二:
1'定義第一級分類
2sub mainfl()
3 dim rs
4 set rs=conn.execute ("select id,F_id,F_name from ClassNae where sid=0 order by id desc")
5 if not rs.eof then
6 do while not rs.eof
7 response.write rs(2) & "<br>"
8 call subfl(rs(0)," |- ") '循環子級分類
9 rs.movenext
10 if rs.eof then exit do '防上造成死循環
11 loop
12 end if
13end sub
14'定義子級分類
15sub subfl(fid,strdis)
16 dim rs1
17 set rs1=conn.execute("select id,sid,ClassName from ClassName where sid="&fid&" order by id desc")
18 if not rs1.eof then
19 do while not rs1.eof
20 response.write rs1(2) & "<br>"
21 call subfl(rs1(0)," "&strdis) '遞歸子級分類
22 rs1.movenext
23 if rs1.eof then
24 rs1.close
25 exit sub
26 end if
27 loop
28 end if
29end sub