ASP TO HTML WITH TEMPLATE (3)

5,处理数据功能显示页面addit.asp

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

1'容错处理 On Error Resume Next 
 1'接受传递值 c_title=request.form("c_title") c_content=request.form("c_content") 
 2``` ```
 3'生成HTML文件名,建立文件夹,指定文件路径 fname = makefilename(now()) 'makefilename为自定义函数 folder = "newsfile/"&date()&"/" filepath = folder&fname 
 4``` ```
 5'将接受值及路径保持至数据库表 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 
 6``` ```
 7'打开模板代码,并将其中特殊代码转变为接受值 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 $",now()) mb_code=replace(mb_code," $cnleft $",c_title) mb_code=replace(mb_code," $cnright $",c_content) 
 8``` ```
 9'生成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 
10``` 文章添加成功,

浏览

1[Ctrl+A 全部选择 然后拷贝] 
2
3**6,显示数据库表记录,并做指向HTML页的链接:showit.asp**
4<!--#include file="conn.asp" -->
5<!--#include file="lib.asp" -->

Set rs = Server.CreateObject ("ADODB.Recordset") sql = "Select * from c_news order by c_id desc" rs.Open sql,conn,1,1 if rs.EOF and rs.BOF then response.write ("暂时还没有文章,

1&lt;a href="add.html"&gt;添加&lt;/a&gt;

") else Do Until rs.EOF

``` =rs("c_time") `````` =rs("c_title") ```
[Dell][Edit][Add]``` =htmlencode(rs("c_content")) ```
``` ```
``` ``` rs.MoveNext Loop end if ``` ``` rs.close Set rs = Nothing conn.close set conn=Nothing ```

[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]

7,修改数据内容页chang.asp

修改数据内容,同时也需要修改更新对应的HTML页面。修改其实就是重新生成文件,且文件名和之前一样,类似文件的覆盖。

 1id=request.querystring("c_id")
 2``` ```
 3 if request.form("submit")="change" then c_title=request.form("c_title") c_content=request.form("c_content") c_id=request.form("c_id") c_filepath=request.form("c_filepath") Set rs = Server.CreateObject ("ADODB.Recordset") sql = "Select * from c_news where c_id="&c_id rs.Open sql,conn,3,2 rs("c_title")=c_title rs("c_content")=c_content rs("c_time")=now() rs.update rs.close Set rs = Nothing 
 4``` ```
 5'打开模板代码,并将其中特殊代码转变为接受值 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 $",now()) mb_code=replace(mb_code," $cnleft $",c_title) mb_code=replace(mb_code," $cnright $",c_content) 
 6``` ```
 7'生成HTML页面 Set fso = Server.CreateObject("Scripting.FileSystemObject") Set fout = fso.CreateTextFile(Server.MapPath(c_filepath)) fout.WriteLine mb_code fout.close 
 8``` ```
 9response.redirect("showit.asp")
10``` ```
11end if
12``` ```
13 if id<>"" then Set rs = Server.CreateObject ("ADODB.Recordset") sql="select * from c_news where c_id="&id rs.Open sql,conn,1,1 c_id=rs("c_id") c_filepath=rs("c_filepath") c_title=rs("c_title") c_content=rs("c_content") end if 
1<form action="change.asp" method="post"> Title:<input =c_title="" ```="" name="c_title" type="text" value="```"/><br/> Content:<br/> <textarea cols="30" name="c_content" rows="8">```
2=c_content
3```</textarea><br/> <input name="submit" type="submit" value="change"/> <input type="reset" value="Reset"/> <input name="c_id" type="hidden" value="```
4=id
5```"/> <input name="c_filepath" type="hidden" value="```
6=c_filepath
7```"/> </form>

[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]

8,删除记录页del.asp

同样!删除,除了删除数据库表中的记录,与其对应的HTML页面也需删除。代码如下:

 1   
 2c_id = request.querystring("c_id")   
 3sql = "Select * from c_news where c_id="&c_id   
 4Set rs = Server.CreateObject ("ADODB.Recordset")   
 5rs.Open sql,conn,2,3 
 6
 7filepath=rs("c_filepath")   
 8Set fso = CreateObject("Scripting.FileSystemObject")   
 9fso.DeleteFile(Server.mappath(filepath))   
10Set fso = nothing 
11
12rs.delete   
13rs.close   
14Set rs = Nothing   
15conn.close   
16set conn=nothing   
1response.redirect("showit.asp")

四,其它功能

模板管理页面: 不会每次都是打开数据库表进行增加或者修改模板代码吧,所以,管理代码的页面程序不能少了,自己捣鼓下应该很简单的。当然,之前管理员的登录认证程序就不在书中交代了:)还有,如果设计了多个模板,那么在发表信息的时候应添加模板选择单选框,同样在执行转换HTML时,SQL选择的不同m_id了。不管怎么说,先把这些技术自己调试感受下。多多操作,相信“读书千遍,其意自见”。

调试地址: www.cnbruce.com/test/asp2html/showit.asp

文件下载: www.cnbruce.com/test/asp2html/asp2html.rar

Published At
Categories with Web编程
Tagged with
comments powered by Disqus