ASP universal program to implement collapsible menu in web pages
Bank of China Shanxi Province Jincheng Branch Science and Technology Branch Jinyu
With the increasing popularity of the Internet/Intranet, Web programming and web page production have become a trend. This article introduces readers to a programming technology for implementing folding menus in web pages. I believe it will add a lot of color to your web pages. The so-called folding menu is actually a dynamically displayed menu, that is, when no menu operation is performed, only the main menu is displayed. When a menu item is selected, its subordinate submenus are dynamically displayed, and after the selection is completed, they are hidden again.
Implementation principle
You must be very familiar with the HTML <DIV> tag. We can use its display attribute to hide or display the content of the <DIV> tag. The specific method is to hide or display when display is set to none. If we have marked all the menu names (including submenus) with <DIV>, we can realize the folding menu by using ASP language combined with Script to dynamically control the display or hiding of the corresponding menu options.
The problem now is how to add the menu item name to the program. Of course, you can add it directly to the web page, but if you change the menu option, you must re-modify the control code of the web page. This method is obviously not wise enough. What this article uses is to store all the menu option names in a text file in a certain format. When the web page is loaded, the ASP code automatically reads its content. In this way, if the menu options are changed, just make corresponding changes in this file. Can.
Regarding file operations, we use the built-in file access component of ASP to complete it. For specific usage, see the program code at the end of the article.
menu text file
This article agrees that the content of the menu text file must follow the following rules: the name describing a menu option must have three lines of content (see the example below); no blank lines are allowed in the file; there can be spaces before the menu option name, but the space key must be used (space key) ), the Tab key cannot be used; the end of the file is completed with two lines of *END*.
Suppose there is the following three-level menu:
operating system software
Computer software----Application system software
Tool software-----PC TOOLS
CuteFTP
The menu text should have the following format:
1------indicates the first main menu name
Computer software--------Menu name (the same below)
3 If it is not 0, specify the number of submenus of this menu; in this example, it is 3
1*1 indicates the first submenu of the first main menu (* must be used)
operating system software
0 http://… --------- If it is 0, it means that the menu item has no submenu, followed by the hyperlink URL
1*2---------- indicates the second submenu of the first main menu (the same below)
application system software
0 http://…
1*3
Tool software
2
1*3*1-------- The 1st submenu of the 3rd submenu in the 1st main menu
PC TOOLS
0 http://…
Program code:
<HTML>
<HEAD>
<SCRIPT Language=VBScript>
'Show or hide submenu
Sub disp_sub_menu(curid)
dim ct,i,tmpid
ct=document.all(curid).style.ct
i=1
While i<=CInt(ct)
tmpid=curid+*+cstr(i)
If document.all(tmpid).style.display=none Then
document.all(tmpid).style.display=
Else
document.all(tmpid).style.display=none
If CInt(document.all(tmpid).style.ct)>0 Then
If document.all(tmpid+*1).style.display= Then
disp_sub_menu(tmpid) 'Recursively call lower-level submenu
End If
End If
End If
i=i+1
Wend
End Sub
</SCRIPT></HEAD><BODY>
<FONT color=red><H2 align=center>Example of implementing folding menu in web page using ASP</H2></FONT><HR>
<% '----| Calculate the * number in menu id|-----
Function spnum(str)
dim tmpstr,m,t
tmpstr=str
m=InStr(str,*)
t=0
While m<>0
t=t+1
tmpstr=Mid(tmpstr,m+1)
m=InStr(tmpstr,*)
Wend
spnum=t
End Function
'-----| Send a menu option to the browser |-----
Sub output_line(ct_flag,curid,txtname,ct,txtcolor)
dim ptl,sp,dispval,tspace
sp=spnum(curid)
dispval=none
If sp=0 Then dispval=
tspace=
While sp>0
tspace=tspace+
sp=sp-1
Wend
If ct_flag=1 Then 'This level menu has submenus, only use <DIV> to mark them
ptl=<div id=+chr(34)+curid+chr(34)+ style=
ptl=ptl+chr(34)+color:+txtcolor+;
ptl=ptl+ ct:+ct+; line-height:25px;
ptl=ptl+ cursor:hand;
ptl=ptl+ display:+dispval+chr(34)
ptl=ptl+ onclick=+chr(34)+disp_sub_menu('+curid+')+chr(34)
ptl=ptl+>+tspace+txtname+</div>+chr(13)
Else 'This level menu is the lowest level menu, marked with <DIV> and <A>
ptl=<div id=+chr(34)+curid+chr(34)
ptl=ptl+ style=+chr(34)+display:+dispval+;
ptl=ptl+line-height:25px; color:+txtcolor+; ct:0+chr(34)+>
ptl=ptl+tspace+<A href=+chr(34)+ct+?txt=+txtname+chr(34)+>+txtname+</A></div>+chr(13)
End If
response.write ptl
End Sub
'----| Main control process|-----
dim curid,txtname,ct,ct_flag,txtcolor
set fs=createobject(SCRIPTING.FILESYSTEMOBJECT)
menufile=replace(request.servervariables(path_translated),menu.asp,mfile.txt)
set txtstr=fs.opentextfile(menufile)
curid=txtstr.readline
While curid<>*END*
curid=y+Trim(curid) 'Form the id of the current menu item
txtname=Trim(txtstr.readline) 'Read menu name
ct=Trim(txtstr.readline) 'Read the number of submenus of this menu
ct_flag=1
If Mid(ct,1,1)=0 Then
ct_flag=0
ct=LTrim(Mid(ct,2))
End If
txtcolor=black
Select case spnum(curid)
case 1
txtcolor=red 'Level 1 submenu color a
case 2
txtcolor=green 'Second-level submenu color
case 3
txtcolor=blue 'Three-level submenu color, can continue to be added
End Select
output_line ct_flag,curid,txtname,ct,cstr(txtcolor)
curid=txtstr.readline
Wend
txtstr.close
%>
<HR></BODY></HTML>
This code passes under Win98/PWS (Personal Web Server).