利用ACCESS创建数据库listBase.mdb:
数据表:listTable
数据字段:listId listName listInfo parentId 连接方法:
<%
dim sConn,sConnStr,sSql
sConnStr="DRIVER={Microsoft Access Driver (*.mdb)};dbq=" & server.mappath("listBase.mdb")
set sConn=server.createobject("adodb.connection")
sConn.open sConnStr
%>
把以上代码保存成文件:initialize.asp 下面这段程序代码是用来显示目录树用的,保存成文件:inc.asp
<!--#include file="initialize.asp"-->
<script language=jscript>
<!--
//此函数用来显示与关闭它的子菜单的,并切换其前面的小图片
function showMenu(menuObj,isSub){
if(menuObj.style.display=="none"){
menuObj.style.display="";
if(isSub==1){
event.srcElement.src="images/open.gif";
}
}
else{
menuObj.style.display="none";
if(isSub==1){
event.srcElement.src="images/close.gif";
}
}
}
//-->
</script>
<!--下面这段蓝色的区域,是设置目录树的根,其中的内容非数据内的数据,数据库内的数据都附着在它的下面//-->
<%
response.write("<table width='100%' border='0' cellspacing='0' cellpadding='0'>" & chr(13))
response.write("<tr>" & chr(13)&"<td>" & chr(13) & "<table width='100%' border='0' cellspacing='0' cellpadding='0'>")
response.write("<tr>" & chr(13) & "<td width='18' ><img src='images/root.gif' onclick='jscript:showMenu(subMenu0,0)' style='cursor:hand' alt='不可修改'></td>" & chr(13) & "<td width='100' class='menuPro' id='listName0'>根节点</td>" & chr(13))
response.write("<td width='40' class='menuFont' align='center' style='cursor:hand' onclick=""jscript:showForm('add',0)"">添加</td>" & chr(13))
response.write("<td width='40' class='menuFont' align='center' onclick=''></td>" & chr(13))
response.write("<td> </td></tr></table></td></tr>" & chr(13))
response.write("<tr id='subMenu0'>" & chr(13) & "<td colspan='2'>" & chr(13))
'此处调用显示目录树的递归函数
listChild(0)
response.write("</td>" & chr(13) & "</tr>" & chr(13) & "</table>" & chr(13))
'下面代码为显示目录树最关键的函数,使用了递归方法(很多象这样类型的都是用递归来做的。)
function listChild(id)
dim sRec,sSql,sNum,sTmp
set sRec=server.createobject("adodb.recordset")
sSql="select listId,listName,listInfo,parentId from listTable where parentId=" & id
sRec.open sSql,sConn,3,1
if not sRec.eof then
sNum=1
response.write("<table width='100%' border='0' cellspacing='0' cellpadding='0'>" & chr(13))
while not sRec.eof
sTmp=sConn.execute("select count(listId) as countSub from listTable where parentId=" & sRec("listId"))
if sTmp("countSub")>0 then
response.write("<tr>" & chr(13))
if sNum=sRec.recordcount then
response.write("<td width='18' valign='top'><img src='images/angel.gif'></td>" & chr(13))
else
response.write("<td width='18' background='images/eline.gif' valign='top'><img src='images/t.gif' ></td>" & chr(13))
end if
response.write("<td>" & chr(13) & "<table width='100%' border='0' cellspacing='0' cellpadding='0'>" & chr(13))
response.write("<tr>" & chr(13) & "<td width='118' class='menuPro' id='listName" & sRec("listId") & "'><img id='img" & sRec("listId") & "' style='cursor:hand' src='images/close.gif' align='absMiddle' onclick='jscript:showMenu(subMenu" & sRec("listId") & ",1)' alt='" & sRec("listInfo") & "'>" & server.HTMLEncode(sRec("listName")) & "</td>" & chr(13) & "<td width='40' class='menuFont'><div align='center' style='cursor:hand' onclick=""jscript:showForm('add'," & sRec("listId") & ")"">添加</div></td><td width='40' class='menuFont'><div align='center' style='cursor:hand' onclick=""jscript:showForm('edit'," & sRec("listId") & ")"">编辑</div></td>" & chr(13))
response.write("<td width='40' class='menuFont'><div align='center' style='cursor:hand' onclick=""jscript:window.location='disp.asp?disp=del&parentId=" & sRec("listId") & "'"">删除 </div></td>" & chr(13) & "<td> </td>" & chr(13) & "</tr>" & chr(13) & "</table>" & chr(13) & "</td>" & chr(13) & "</tr>" & chr(13))
response.write("<tr id='subMenu" & sRec("listId") &"' style='display:none'>")
if sNum=sRec.recordcount then
response.write("<td width='18' valign='top'")
response.write("<img src='images/angel.gif' >")
response.write("</td>" & chr(13))
else
response.write("<td width='18' valign='top' background='images/eline.gif'>")
response.write("</td>" & chr(13))
end if
response.write("<td>" & chr(13))
listChild(sRec("listid"))
response.write("</td>" & chr(13) & "</tr>" & chr(13))
else
response.write("<tr>" & chr(13))
if sNum=sRec.recordcount then
response.write("<td width='18' valign='top'><img src='images/angel.gif'></td>" & chr(13))
else
response.write("<td width='18' background='images/eline.gif' valign='top'><img src='images/t.gif' ></td>" & chr(13))
end if
response.write("<td>" & chr(13) & "<table width='100%' border='0' cellspacing='0' cellpadding='0'>" & chr(13))
response.write("<tr>" & chr(13) & "<td width='118' class='menuPro' id='listName" & sRec("listId") & "'><img id='img" & sRec("listId") & "' src='images/file.gif' align='absMiddle' alt='" & sRec("listInfo") & "'>" & server.HTMLEncode(sRec("listName")) & "</td>" & chr(13) & "<td width='40' class='menuFont'><div align='center' style='cursor:hand' onclick=""jscript:showForm('add'," & sRec("listId") & ")"">添加</div></td><td width='40' class='menuFont'><div align='center' style='cursor:hand' onclick=""jscript:showForm('edit'," & sRec("listId") & ")"">编辑</div></td>" & chr(13))
response.write("<td width='40' class='menuFont'><div align='center' style='cursor:hand' onclick=""jscript:window.location='disp.asp?disp=del&parentId=" & sRec("listId") & "'"">删除</div></td>" & chr(13) & "<td> </td>" & chr(13) & "</tr>" & chr(13) & "</table>" & chr(13) & "</td>" & chr(13) & "</tr>" & chr(13))
end if
sRec.movenext
sNum=sNum+1
wend
response.write("</table>" & chr(13))
end if
sRec.close '关闭记录集
set sRec=nothing '关闭记录集
end function
sConn.close '关闭数据库链接
set sConn=nothing '关闭数据库链接
%>
下面这是调用页代码,保存成index.asp
<%@LANGUAGE='VBSCRIPT' CODEPAGE='936'%>
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>
<title>目录树</title>
<script language='JavaScript' type='text/JavaScript'>
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=='Netscape')&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW
innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//-->
</script>
<script language="JScript">
<!-- //此函数设置处理表单的内容
//比如添加时,表单为空,并显示为什么节点添加子节点
//比如修改时,表单内为相应节点的内容,并显示在修改什么节点
function showForm(disp,parentId){
addNodeDiv.style.display="";
document.all.addNodeForm.disp.value=disp;
document.all.addNodeForm.parentId.value=parentId;
if(disp=="edit"){
document.all.addNodeForm.listInfo.value=eval("img"+parentId+".alt");
document.all.addNodeForm.listName.value=eval("listName"+parentId+".innerText");
infoValue.innerText="编辑["+eval("listName"+parentId+".innerText")+"]节点";
}
else{
document.all.addNodeForm.listInfo.value="";
document.all.addNodeForm.listName.value="";
infoValue.innerText="给节点["+eval("listName"+parentId+".innerText")+"]添加子节点";
}
}
<!--这棕色部分为移动表单代码,可以不要。//-->
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
} function MM_dragLayer(objName,x,hL,hT,hW,hH,toFront,dropBack,cU,cD,cL,cR,targL,targT,tol,dropJS,et,dragJS) { //v4.01
//Copyright 1998 Macromedia, Inc. All rights reserved.
var i,j,aLayer,retVal,curDrag=null,curLeft,curTop,IE=document.all,NS4=document.layers;
var NS6=(!IE&&document.getElementById), NS=(NS4
NS6); if (!IE && !NS) return false;
retVal = true; if(IE && event) event.returnValue = true;
if (MM_dragLayer.arguments.length > 1) {
curDrag = MM_findObj(objName); if (!curDrag) return false;
if (!document.allLayers) { document.allLayers = new Array();
with (document) if (NS4) { for (i=0; i<layers.length; i++) allLayers[i]=layers[i];
for (i=0; i<allLayers.length; i++) if (allLayers[i].document && allLayers[i].document.layers)
with (allLayers[i].document) for (j=0; j<layers.length; j++