Abstract: We introduce several tricks when using Active Server Pages to design homepages. These include the creation and automatic updating of news columns, as well as the creation, utilization and maintenance of bulletins. The general solutions to these problems are pointed out.
关键词:ASP、新闻栏、公告栏
ASP是Microsoft公司推出的一种用以取代通用网关接口的技术,英文全称Active Server Pages。它是一个Web服务器端的运行环境。ASP本身包含了VBScript和javascript引擎,使得脚本可以直接嵌入HTML中。现将我们在ASP运用过程中的几个小技巧与大家分享。
<% Dim fs, f, f1, fc, filenum dim count dim i,j,t1,t2 dim file(100) dim mystr(100) '标志文件中日期的字符串 count=0 Set fs = CreateObject("Scripting.FileSystemObject") Set f = fs.GetFolder("D:/ASP/news ") Set fc = f.Files for each f1 in fc count=count+1 '计文件个数 file(count)=fs.getbasename(f1) '标记文件基名 mypos=InStr(1, file(count), "(") ')在文件基名中的位置 '文件基名中的日期字符串 mystr(count) = Mid(file(count), mypos + 1, Len(file(count)) - (mypos + 1)) next for i=1 to count for j=1 to count-i if mystr(j)<mystr(j+1) then t1=file(j) file(j)=file(j+1 file(j+1)=t1 t2=mystr(j) mystr(j)=mystr(j+1) mystr(j+1)=t2 end if next next j=1 if count<10 then '新闻栏中共显示10条新闻 temcount=count else temcount=10 end if do while j<=temcount%> <a href="file:///D:/ASP/news/<%=file(j)%>.htm" target="new_window"><%=file(j)%> </a> <% j=j+1 loop %>
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <meta name="GENERATOR" content="Microsoft FrontPage 4.0"> <meta name="ProgId" content="FrontPage.Editor.Document"> <title>更新公告栏内容</title> </head> <body> <script language=vbscript> function datacheck() dim msg,errflag errflag=true if len(trim(maintain.t1.value))=0 then focusto(0) errflag=false msg="请输入需提交的公告内容" end if if (errflag=false) then msgbox msg,64,"oh no!" exit function end if datacheck=errflag maintain.submit end function sub focusto(x) document.maintain.elements(x).focus() end sub </script> ①<form method="POST" action="公告.asp" name="maintain"> <p><input type="text" name="t1" size="84"><input type="reset" value="重写" name="B2"> <input type="button" value="提交" name="B1" onclick="datacheck()"></p> <hr color=#FF99FF size=1> </form> </body> </html>
<% dim str str=request.form(t1) dim s(5) const forreading=1,forwriting=2 dim fso,myfile set fso=server.createobject("scripting.FileSystemObject") set myfile=fso.opentextfile("维护文本.txt",forreading)'以Reading模式打开文件 for i=1 to 5 '公告栏共显示5条公告 s(i)=myfile.readline'读取文件内容 next myfile.close set myfile=fso.opentextfile("维护文本.txt",forwriting,true) '以writing模式打开文件 myfile.writeline str for i=1 to 4 myfile.writeline s(i) '将数据写回文件 next myfile.close %>