<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>asp无限级调用分类</title>
<style>
.class1{color:#FD0509; font-size:16px;}
.class2{color:#3B02FB; font-size:14px;}
.class3{color:#54FB05; font-size:14px;}
</style>
</head>
<body>
<%
dim startime,cn,connstr,db
startime=timer()
db="db.mdb" '数据库
'on error resume next '尝试连数据库,一直到超时,但可以加强SQL注入过滤
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(db)
'connstr="DBQ="+server.mappath(""&db&"")+";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
set cn=server.createobject("ADODB.CONNECTION")
cn.open connstr
levelCount=1
sql="select * from Menu where ParentId='0' and Status=1 order by OrderNum ASC"
set rs=server.CreateObject("adodb.recordset")
rs.open sql,cn,1,1
do while not rs.eof
%>
<div><a href="<%=rs("url")%>" class="class1"><%=rs("title")%></a></div>
<%
call getChildMenu(rs("MenuId")," ",levelCount+1)
rs.movenext
loop
rs.close
set rs=nothing
function getChildMenu(ParentId,LevelString,levelCount)
sql="select * from Menu where ParentId='"&ParentId&"' and Status=1 order by OrderNum ASC"
set rs_ChildMenu=server.CreateObject("adodb.recordset")
rs_ChildMenu.open sql,cn,1,1
do while not rs_ChildMenu.eof
classString="class"&levelCount
if levelCount>3 then '当预设样式不足的情况下,设置超出级别的样式
classString="class3"
end if
%>
<div><%=LevelString%>|_ <a href="<%=rs_ChildMenu("url")%>" class="<%=classString%>"><%=rs_ChildMenu("title")%></a></div>
<%
call getChildMenu(rs_ChildMenu("MenuId"),LevelString&" ",levelCount+1)
rs_ChildMenu.movenext
loop
rs_ChildMenu.close
set rs_ChildMenu=nothing
end function
%>
<br>
//=============================<br>
Menu表中<br>
MenuId 为菜单编号,必须唯一。建此此菜单使用类似本DEMO中所使用具有一定规则的编号,方便将来的权限管理使用<br>
ParentId 为当前菜单的父菜单编号,当此字段值为0则该菜单为一级菜单<br><br>
本例递归函数加入levelCount参数来确定当前递归的层数,可使用此参数判断当前第几级,并且可控制当前级别调用的样式表<br>
</body>
</html>