ASP+FSO+框架實現ASP生成htm靜態頁並分頁的方法

其實網上已經有很多ASP生成htm的文章了
,有一種方法是ASP+XML的生成方法,雖然有一種好處就是不用程序寫模版就可以直接引用原來的要生成頁面源碼使用,但
本人進行此方法測試時,發現其穩定性差和網速要求高(當然不是在服務器上了)。特別是虛擬空間上經常報錯,有可能在本
人在此方法上代碼處理不足的原因吧。長話短說,這篇文章使用大家常用的另一種方法ASP+FSO,這裏還應用了框架就是爲
了處理大量分頁時減少生成時間使用的,這種方法是針對一些頁面量較大的ASP文件。
這裏我引用一個簡單實例:(旅遊電子商務)全國各大城市酒店應用靜態頁(htm)分頁顯示
1.應用系統環境:win2000+ASP+MSSQL/ACCESS(數據庫基本沒有關係了通用的)+iis5.0
2.1個城市列表(CityHtml):包括定義靜態htm名稱共三個字段(城市ID(自動編號),城市名稱(CityName例如北京),生成htm前綴名(HtmlStartName例如beijing))
3.1個全國酒店列表(Hotel):這裏我只建立三個字段(酒店ID(自動編號),城市名稱(City),酒店名稱(HotelName))方便來引用實例。
4.1個ASP頁面(ToHtm.asp)(生成htm使用)
5.1個循環框架頁面(IframeToHtm.asp),應用框架批量生成htm
以下給出兩個頁面的源碼
循環框架進行批量生成的頁面:IFrameToHtm.asp
<!--#include file="conn.asp"-->'連接數據庫
<%
dim rs,sql,j
set rs=Server.CreateObject("adodb.recordset")
sql="select * from CityHtml"'打開全國城市列表
rs.open sql,conn,1,1
do until rs.eof'循環各大城市%>
<!--以下應用框架打開ToHtml生成頁面-->
<IFRame name="LoadRcHtm<%=j%>" frameborder=0 width=100% height=30 scrolling=no src="ToHtml.asp?City=<%=cstr(rs("city"))%>&HtmlStartName=<%=rs("HtmlStart")%>"></IFrame>
<%rs.movenext
loop%>
生成程序頁面:ToHtm.asp 我在源碼大概寫上註釋**
<!--#include file="conn.asp"-->'數據連接文件
<%
On Error Resume Next'容錯處理
Dim City'定義取得要生成頁面的城市
City=Request.Querystring("City")'獲取生成的城市酒店值從框架傳過來的在後面將介紹
HtmlStartName=Request.Querystring("HtmlStartName")'獲得生成htm文件名前綴
Dim sql'搜索字符串,這裏我就直接打開表不用搜索變量了,搜索條件按自己寫就可以
sql="select * from Hotel where [City] = '" & City & "' "
Dim oRs'數據操作對象
Dim PageCounts'實現分頁生成必須得知呀有多少頁
Set oRs = Server.CreateObject("ADODB.Recordset")
oRs.Open Sql,oConn,1,1'找開酒店等於City變量的表
oRs.pagesize=10'十個記錄爲一頁
PageCounts=oRs.pagecount'得出要生成多少個頁面,循環生成使用
Dim fs'定義fso文件對象
Dim folders'存放生成靜態頁的文件夾名稱
Dim Filestart'定義生成htm文件前綴
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Dim i
for i=1 to PageCounts'開始循環生成頁面,也就是分頁生成了
page=i
oRs.absolutepage=i'頁碼
rowcount=oRs.pagesize'當頁記錄數
folders=server.mappath("CityHtml")
if (fs.FolderExists(folders)) then'判斷文件夾是否存在
else
fs.CreateFolder(folders)'不存在則創建CityHtml文件夾
end if
if i=1 then
Filestart=HtmlStartName'如果爲第一頁則定義文件名爲傳值名.例如beijing則爲beijing.htm
else
Filestart=HtmlStartName&i'如果第二頁則爲beijing+1例如有兩頁也就是i等於2則爲 beijing2.htm如此類推...(.htm後綴就在後面加上)
end if
Dim files'定義生成文本文件名稱變量
Dim filez'定義文件路徑名稱變量
files=Filestart&".txt"'本文件名稱
filez=folders&"\"&"files'文本文件路徑
'冊除文件Dim checkfile'檢查文本文件是否已經存在,是則刪除
checkfile=server.mappath("CityHtml\"&Filestart&".htm")'檢查htm文件是否已經存在,是則刪除
if (fs.FileExists(checkfile)) then'檢查htm文件是否已經存在,是則刪除
Dim df'定義文件對象*刪除文件使用*
Set df=fs.GetFile(checkfile)'定義要冊除的文件
df.delete'冊除文件
end if'判斷結束
Dim ts'定義寫入文件對象
set ts = fs.createtextfile(filez,true) '開啓寫入文件內容**我在正文只簡單寫入酒店名稱和靜態數字分頁顯示**
ts.write("<Html><Head><Title>生成"&City&"城市酒店</Title>"&vbcrlf)'之後就是要生成的正文件內容了跟使用Response.write
ts.write("<META http-equiv=Content-Type content=text/html; charset=gb2312>"&vbcrlf)
ts.write("<meta name=keywords content="&city&"酒店>"&vbcrlf)
ts.write("<link href='/Style/style.css' rel='stylesheet' type='text/css'></head><body topmargin=0>"&vbcrlf)
ts.Write("<TABLE WIDTH=760 cellspacing=0 cellpadding=0 align=center>"&vbcrlf&_
"<TR><TD width='100%'>"&vbcrlf)
'分頁輸出開始
'數字分頁程序原理在這我就不多說了,不懂的朋友可在網上搜索一下
Dim page'當前頁
Dim Page2'數字分頁變量
Dim s'數字分頁變量
if page=1 then
ts.write (" [首 頁] [前一頁] ")
else
ts.write (" <a href="&HtmlStartName&".htm"&" class=blue>[首 頁]</a> <a href="&HtmlStartName&Replace(page-1,1,"")&".htm"&" class=blue>前一頁</a> ")
end if
page2=(page-(page mod 10))/10
if page2<1 then page2=0
for s=page2*10-1 to page2*10+10
if s>0 then
if s=cint(page) then
ts.write (" <font color='#000000'>["& s & "]</font>")
else
if s=1 then
ts.write (" <a href="&HtmlStartName&replace(s,1,"")&".htm"&" class=blue>["& s &"]</a>")
else
ts.write (" <a href="&HtmlStartName&s&".htm"&" class=blue>["& s &"]</a>")
end if
end if
if s=ors.pagecount then
exit for
end if
end if
next
if cint(page)=ors.pagecount then
ts.write (" [後一頁] [尾 頁]")
else
ts.write (" <a href="&HtmlStartName&page+1&".htm"&" class=blue>[後一頁]</a> <a href="&HtmlStartName&ors.pagecount&".htm"&" class=blue>[尾 頁]</a>")
end if
ts.write("</TD></TR>")
'分頁輸出結束
do while not ors.eof and rowcount>0 '輸出酒店名稱
ts.write("<TR><TD width='100%'>"&oRs.Fields("Chinese_Name")&"</TD></TR>"&vbcrlf)
oRs.movenext
rowcount=rowcount-1'當頁記錄數-1 loop
ts.write("</Table></body></html>"&vbcrlf)
ts.close
set ts=nothing '釋放對象
Dim EditFile'定義改寫文件變量
Set EditFile = fs.GetFile(filez)'設置改寫文件對象
EditFile.name= left(EditFile.name,len(EditFile.name)-4)&".htm" '改寫文本文件成htm
next'循環生成結束(分頁生成)
set EditFile=nothing '釋放對象
set fs=nothing'釋放對象
if err.number<>0 then '處理生成錯誤
Response.write(City&"更新時發生未知錯誤<A href=ToHtml.asp?City="&City&"&HtmlName="&HtmlStartName&">重新更新</A>")
else
Response.Write(City&"酒店更新已完成 "&Now())
end if
%>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章