還是第一次在CSDN寫文章,本人文采和理論知識有限,寫得不正確的地方歡迎指正。其實網路上已經有很多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
%>