| 
	| 〖文章分类:电脑·手机·网络 / 网站设计·开发·优化〗〖阅读选项〗 |  | 还是第一次在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
 %>
 
 |  | 文章作者:未知  文章来源:CSDN博客  更新日期:2009-06-16 |  | 〖文章浏览:〗〖发送文章〗〖打印文章〗 |  |  |  |