續上篇 上一篇中,我們實現了樹型菜單的顯示,通過一個遞歸的子程序來 實現的。現在我們給它加上簡單的管理功能,功能參看第一篇中的功能 設計,主要功能有:增加、刪除、修改。
一、設計思路 為了在一個ASP文件中實現顯示和管理的功能,我們加上一個變量來 判斷所要做的操作,用action表示動作,顯示增加的表單時 action=add, 執行增加時action=doadd ;顯示刪除表單時 action=del,執行刪除時 action=dodel;顯示修改表單時 action=mod ,執行修改時action=domod 沒有任何參數則表示顯示。另外再加上一個參數id,來表示動作的目標。 注意,這里增加是指在id這條記錄下增加一個子菜單。id和action變量直接 用URL來提交,就是get的方法,這樣可以直接用下面的方式調用,比如要修改 id為14的這條記錄,可以用如下的鏈接: <a href='index.asp?action=mod&id=14'>修改</a>
其中index.asp是網頁的文件,如果你的文件名不是index.asp,請自行 修改。 在程序的最開始,我們先判斷action是否有值,如果有,就轉到相應 的子程序進行處理,處理后轉到顯示的頁面。為了通用,文件名不直接給出, 而是在程序中獲取當前的文件名。
二、簡單流程 有了設計思路,我們先來寫出簡單的流程,如下: <%'引用包含文件%> <!--#include file="operation$db.asp" --> <% '獲取action和id變量 action=request.querystring("action") id=request.querystring("id")
'獲取當前文件名 scr=Request.ServerVariables("SCRIPT_NAME")
opendb my '打開數據庫連接 '用一個select case 根據action的值轉到相應的子程序,這里都是執行部份 select case action case "doadd" doadd() '執行增加子程序 doadd case "domod" domodify() '執行修改子程序 domodify case "dodel" dodelete() '執行刪除子程序 dodelete end select
'執行增加子程序 sub doadd() end sub
'執行修改子程序 sub domodify() end sub
'執行刪除子程序 sub dodelete() end sub
'因為執行部份最后都要轉向,所以寫在<html>的前面,下面是網頁HTML部份 %> <html> <head> '這里加入JS腳本,替換這一句 </head> <body> <% '在這里還要判斷action的值是否為顯示動作表單 if action="" then 'action的值已經在最開始就得到了;下面這幾行是原來就有的。 dim i,l,r,cellid,ph dim cellida,cellidb,cellidc l=0 r=0 br=vbcrlf distree(0) end if '判斷是不是要顯示動作表單,是就轉到子程序 select case action case "add" showadd '顯示增加表單 case "mod" showmodify '顯示修改表單 case "del" showdel '顯示刪除表單 end select closedb my '關閉數據庫連接 %> <% '下面開始都是子程序
'顯示增加表單 sub showadd() end sub
'顯示修改表單 sub showmodify() end sub
'顯示刪除表單 sub showdel() end sub
'別忘了原來的顯示樹型菜單的子程序 sub distree(id) end sub %> </body> </html>
三、寫入代碼 現在把整個流程都已經定好了,就差子程序的內容了。下面一個一個來加進去, 先來加入顯示表單的,這樣可以先定好表單中傳遞過來的參數。 顯示增加表單: sub showadd() %> <table width="400" border="0" cellspacing="0" align="center"> <tr> <td align="center" valign="top"> <form name="add" method="post" action="<%=scr%>?action=doadd&id=<%=id%>"> <table border="1" cellspacing="0" cellpadding="0" bgcolor="#99CC99" width="400" class="s12"> <tr valign="middle"> <td width="80" align="right" height="30" class="tt">ID:</td> <td width="120" height="30" class="tt">-</td> <td width="80" height="30" align="right" class="tt">父節點:</td> <td width="120" height="30" class="tt"><%=id%></td> </tr> <tr valign="middle"> <td width="80" align="right" height="30" class="tt">子節點:</td> <td width="120" height="30" class="tt">0</td> <td width="80" height="30" align="right" class="tt">文字:</td> <td width="120" height="30" class="tt"> <input type="text" name="txt" size="15" maxlength="50" class="txtbox"> </td> </tr> <tr valign="middle"> <td width="80" align="right" height="30" class="tt">鏈接:</td> <td colspan="2" height="30" class="tt"> <input type="text" name="link" size="25" maxlength="50" class="txtbox"> </td> <td width="120" height="30" align="center" class="tt"> <input type="submit" name="Submit" value="增加"> </td> </tr> </form> </td> </tr>
<% end sub
顯示修改表單: sub showmodify() sql="select * from treemenu where id="&id searchtable my,sql,rs if not rs.eof then %> <table width="400" border="0" cellspacing="0" align="center"> <tr> <td align="center" valign="top"> <form name="mod" method="post" action="<%=scr%>?action=domod&id=<%=id%>"> <table border="1" cellspacing="0" cellpadding="0" bgcolor="#99CC99" width="400" class="s12"> <tr valign="middle"> <td width="80" align="right" height="30" class="tt">ID:</td> <td width="120" height="30" class="tt"><%=rs("id")%></td> <td width="80" height="30" align="right" class="tt">父節點:</td> <td width="120" height="30" class="tt"><%=rs("par_id")%></td> </tr> <tr valign="middle"> <td width="80" align="right" height="30" class="tt">子節點:</td> <td width="120" height="30" class="tt"><%=rs("chi_id")%></td> <td width="80" height="30" align="right" class="tt">文字:</td> <td width="120" height="30" class="tt"> <input type="text" name="txt" size="15" value="<%=rs("txt")%>" maxlength="50" class="txtbox"> </td> </tr> <tr valign="middle"> <td width="80" align="right" height="30" class="tt">鏈接:</td> <td colspan="2" height="30" class="tt"> <input type="text" name="link" size="25" value="<%=rs("link")%>" maxlength="50" class="txtbox"> </td> <td width="120" height="30" align="center" class="tt"> <input type="submit" name="Submit3" value="修改"> </td> </tr> </form> </td> </tr>
<% end if closetable rs end sub
顯示刪除表單: sub showdel() sql="select * from treemenu where id="&id searchtable my,sql,rs if not rs.eof then %> <table width="402" border="0" cellspacing="0" align="center"> <tr> <td align="center" valign="top" width="400"> <form name="del" method="post" action="<%=scr%>?action=dodel&id=<%=id%>"> <table border="1" cellspacing="0" cellpadding="0" bgcolor="#99CC99" width="400" class="s12"> <tr valign="middle"> <td width="78" align="right" height="30" class="tt">ID:</td> <td width="122" height="30" class="tt"><%=rs("id")%></td> <td width="80" height="30" align="right" class="tt">父節點:</td> <td width="120" height="30" class="tt"><%=rs("par_id")%></td> </tr> <tr valign="middle"> <td width="78" align="right" height="30" class="tt">子節點:</td> <td width="122" height="30" class="tt"><%=rs("chi_id")%></td> <td width="80" height="30" align="right" class="tt">文字:</td> <td width="120" height="30" class="tt"><%=rs("txt")%> </td> </tr> <tr valign="middle"> <td width="78" align="right" height="30" class="tt">鏈接:</td> <td colspan="2" height="30" class="tt"><%=rs("link")%> </td> <td width="120" height="30" align="center" class="tt"> <input type="submit" name="Submit2" value="刪除"> </td> </tr> </form> </td> </tr>
<% end if closetable rs end sub
注意表單里面action=""的寫法,文件名是引用<%=scr%> action和id參數都要在這里寫入。 因為在最開始獲取action和id的時候是用request.querystring的方式,這樣也就不用在表 單里加入隱藏的參數了。 再來寫執行的部份:
執行增加 sub doadd() txt=request.form("txt") '獲取表單中的文本 link=request.form("link") '獲取表單中的鏈接 sql="select * from treemenu where id is null" changetable my,sql,rs rs.addnew '增加一個新記錄 rs("par_id")=id '寫入它的父菜單的id '子菜單數為0,也可以不寫,因為數據表中默認值是0 rs("chi_id")=0 rs("txt")=txt '寫入文本 rs("link")=link '寫入鏈接 rs.update '更新記錄 closetable rs '關閉數據表 '下面兩句是把它的父菜單的子菜單數加1 sql="update treemenu set chi_id = (chi_id+1) where id="& id my.execute sql closedb my '關閉數據庫,因為后面要轉向了。 response.redirect scr '轉到沒有參數的頁面顯示。 end sub
執行修改 sub domodify() txt=request.form("txt") '獲取表單中的文本 link=request.form("link") '獲取表單中的鏈接 '找到這個記錄 sql="select * from treemenu where id ="& id changetable my,sql,rs rs("txt")=txt '修改文本 rs("link")=link '修改鏈接 rs.update '更新記錄 closedb my '關閉數據庫,因為后面要轉向了。 response.redirect scr end sub
執行刪除 sub dodelete() '下面三句先得到數據表的記錄數 opentable my,"treemenu",rs recno=rs.recordcount closetable rs if recno=1 then '如果整個表中只有一個記錄的話,就不能刪除它。 '顯示一個信息,然后轉到 response.write ("<script>alert('靠,只剩這一個記錄了,你還刪??');window.location='<%=scr%>';</script>") else '找到這個記錄 sql="select * from treemenu where id ="& id changetable my,sql,rs parid=rs("par_id") '得到它的父菜單的id chiid=rs("chi_id") '得到它的子菜單的數目 rs.delete '刪除它 rs.update '更新記錄 closetable rs '關閉數據庫表 '下面兩句把它的父菜單的子菜單數減1 sql="update treemenu set chi_id=chi_id-1+"& chiid &" where id="& parid my.execute sql '下面兩句把它的子菜單的父菜單改成它的父菜單 sql="update treemenu set par_id="& parid &" where par_id="& id my.execute sql end if closedb my '關閉數據庫,因為后面要轉向了。 response.redirect scr end sub
最后,要在顯示樹型菜單里顯示出一些鏈接,用于管理。這些部份已經在上篇中寫進distree子 程序里了,只是把這些語句注釋了,現在可以去掉這些注釋了。
四、結束語 到這里為止,整個樹型菜單的顯示、管理功能已經全部完成了。當然還可以加入一 些新的功能比如在第一篇中提到的管理員登錄、菜單的移動、增加的時候可以指定是加入 同級的菜單還是加入子菜單還可以在distree子程序中加入一個參數,來表示顯示的時候 是展開整個樹,還是不展開整個樹。更多的功能就留著朋友們自己去開發吧。
|