网络编程
位置:首页>> 网络编程>> Asp编程>> asp使用模板生成静态页面方法详解(3)

asp使用模板生成静态页面方法详解(3)

作者:cnbruce 来源:蓝色理想 发布时间:2007-09-24 12:29:00 

标签:静态,模板

 3、数据库连接页面conn.asp
  
    完成数据库的字符串连接方法


 <% 
  set conn = Server.CreateObject("ADODB.Connection") 
  connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("asp2html.mdb") 
  conn.Open connstr 
  %> 



4、信息输入页面add.html
  
    其实很简单,就是表单嘛。注意action是跳转到addit.asp
  
    引用:


 <form action="addit.asp" method="post"> 
  Title:<input type="text" name="c_title"><br> 
  Content:<br> 
  <textarea name="c_content" rows="8" cols="30"></textarea><br> 
  <input type="submit" value="Add"> 
  <input type="reset" value="Reset"> 
  </form> 


5、处理数据功能显示页面addit.asp
  
    首先是处理接受过来的数据,并将值写入数据库;接着将模板代码进行引用,并将其 * 殊代码转换为接受值,最终通过FSO生成HTML页面。其中需要注意的还有,生成文件的路径地址保存至数据库表。 
  

<%’容错处理 
  On Error Resume Next 
  %> 
   
  <!--#include file="conn.asp" --> 
  <!--#include file="lib.asp" --> 
   
  <%’接受传递值 
  c_title=request.form("c_title") 
  c_content=request.form("c_content") 
  %> 
   
  <%’生成HTML文件名,建立文件夹,指定文件路径 
  fname = makefilename(now()) ’makefilename为自定义函数 
  folder = "newsfile/"&date()&"/" 
  filepath = folder&fname 
  %> 
   
  <%’将接受值及路径保持至数据库表 
  sql = "Select * from c_news" 
  Set rs = Server.CreateObject ("ADODB.Recordset") 
  rs.Open sql,conn,3,2 
  rs.addnew 
  rs("c_title")=c_title 
  rs("c_content")=c_content 
  rs("c_filepath")=filepath 
  rs.update 
  rs.close 
  Set rs = Nothing 
  %> 
   
  <%’打开模板代码,并将其 * 殊代码转变为接受值 
  sql1="select m_id,m_html from c_moban where m_id=1" 
  set rs1=Server.CreateObject("adodb.recordset") 
  rs1.open sql1,conn,1,1 
  mb_code=rs1("m_html") 
  rs1.close 
  set rs1=nothing 
  conn.close 
  set conn=nothing 
  c_title=htmlencode(c_title) 
  c_content=htmlencode(c_content) 
  mb_code=replace(mb_code,"$cntop{LogContent}quot;,now()) 
  mb_code=replace(mb_code,"$cnleft{LogContent}quot;,c_title) 
  mb_code=replace(mb_code,"$cnright{LogContent}quot;,c_content) 
  %> 
   
  <%’生成HTML页面 
  Set fso = Server.CreateObject("Scripting.FileSystemObject") 
  fso.CreateFolder(Server.MapPath(folder)) 
  Set fout = fso.CreateTextFile(Server.MapPath(filepath)) 
  fout.WriteLine mb_code 
  fout.close 
  %> 



文章添加成功,<a href="showit.asp">浏览</a>

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com