靜態模板頁面 l主要是定義了一些特殊字符用來被替換
<!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN> <html> <head> <meta httpequiv=ContentType content=text/html charset=gb> <title>$Title</title> </head> <body> <div align=center> <br /> $Title</div> <div >浏覽<font color=red><script src=x?NewsId=$NewsId></script></font>次 $Time</div> <div > $Content</div> <div > $Pager</div> <div > <form id=form action=……/AddCommentaspx > <input id=Text type=text /><Img id=Image src=……/……/UserInfo/CheckCodeaspx/><br /> <textarea id=CommentContent cols= rows=></textarea> <br /> <input id=NewsId type=hidden value=$NewsId/> <input id=Button type=submit value=button /> <a ……/Displayaspx?NewsId=$NewsId>查看更多評論</a></form> </div> </body> </html>
前態頁面 NewsAddaspx就是一個表單用來填寫新聞的標題和內容
<%……@ Page Language=C# AutoEventWireup=false validateRequest=false CodeFile=NewsAddaspxcs Inherits=NewsAddAdmin_AdminPanel_NewsAdd %> <%……@ Register TagPrefix=FCKeditorV Namespace=FredCKFCKeditorV Assembly=FredCKFCKeditorV %> <!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>
<html xmlns= > <head runat=server> <title>添加新聞</title> </head> <body> <form id=form runat=server> <div> <aspLabel ID=Label runat=server Text=標題></aspLabel> <aspTextBox ID=Title runat=server Width=px></aspTextBox><br /> <aspLabel ID=Label runat=server Text=內容></aspLabel> <FCKeditorVFCKeditor id=Content basePath=~/FCKeditor/ runat=server Height=px Width=%></FCKeditorVFCKeditor> <aspButton ID=Button runat=server OnClick=Button_Click Text=Button /> <aspLabel ID=Message runat=server ></aspLabel></div> </form> </body> </html>
後台頁面 NewsAddaspxcs
using Systemusing SystemDatausing SystemConfigurationusing SystemCollectionsusing SystemWebusing SystemWebSecurityusing SystemWebUIusing SystemWebUIWebControlsusing SystemWebUIWebControlsWebPartsusing SystemWebUIHtmlControlsusing Mysqlserverusing SystemIOusing SystemTextnamespace NewsAdd……{ public partial class Admin_AdminPanel_NewsAdd SystemWebUIPage……{ protected void Page_Load(object sender EventArgs e)
……{
} protected void Button_Click(object sender EventArgs e)
……{ string strDate = DateTimeNowToString(yyMMdd) + \ + DateTimeNowToString(yyyymmddhhmmss)string strFileName = strDate + shtml//存儲到數據庫中string strTitle=RequestForm[Title]ToString()Trim()//接收傳過來的標題string strContent=RequestForm[Content]ToString()Trim()//接收傳過來的內容string[] content = strContentSplit(new Char[] ……{|})//對內容進行拆分並保存到數組int upbound = contentLength//數組的上限SqlServerDataBase db = new SqlServerDataBase()bool success = dbInsert(insert into inNews(TitleContentFilePath)values( + strTitle + + strContent + + strFileName + ) null)//if (success)
// MessageText = 添加成功!/**////////////////////////////創建當前日期的文件夾開始string dir = ServerMapPath(……/……/+NewsFiles/+DateTimeNowToString(yyMMdd))//用來生成文件夾if (!DirectoryExists(dir))
……{ DirectoryCreateDirectory(dir)} /**////////////////////////////創建當前日期的文件夾結束try……{ for (int i = i < contentLength i++)
……{ //string[] newContent = new string[]//定義和html標記數目一致的數組StringBuilder strhtml = new StringBuilder()
//創建StreamReader對象using (StreamReader sr = new StreamReader(ServerMapPath(……/……/ + NewsFiles/) + \lEncodingGetEncoding(gb)))
……{ String oneline//讀取指定的HTML文件模板while ((oneline = srReadLine()) != null)
……{ strhtmlAppend(oneline)} srClose()}
//為標記數組賦值//SqlServerDataBase db = new SqlServerDataBase()DataSet ds = dbSelect(select top NewsId from inNews order by NewsId desc null)//獲取id string strTable = <table><tr><td>$upUrl</td><td>$Number</td><td>$downUrl</td></tr></table>//上下頁表格注意此處的$upUrl(上一頁)$Number(頁碼分頁)$downUrl(下一頁)
//這三個是用來替換的
string FilePath=strhtml = strhtmlReplace($Title strTitle)strhtml = strhtmlReplace($NewsId dsTables[]Rows[][NewsId]ToString())strhtml = strhtmlReplace($Time DateTimeNowToString(yyyy/MM/dd))strhtml = strhtmlReplace($Content content[i])string strNumber = //數字分頁……
for (int m = m <=upbound m++)
……{ if (m == )//如果是第一頁就顯示成這個樣子shtml而不是_shtml strNumber = strNumber + [+<a + ……/ + strDate + shtml + > + m + </a>+] else……{ int n = m //第三頁的連接應該是_shtml以此類推strNumber = strNumber + [ +<a + ……/ + strDate + _ + n + shtml + > + m + </a>+] } if (upbound == )//如果沒有分頁就直接按日期時間保存……{ FilePath = ServerMapPath(……/……/) + NewsFiles + // + strDate + shtmlstrhtml = strhtmlReplace($Pager )} else//否則按shtml_shtml 這種效果保存……{ if (i == )
FilePath = ServerMapPath(……/……/) + NewsFiles + // + strDate + shtmlelse FilePath = ServerMapPath(……/……/) + NewsFiles + // + strDate + _ + i + shtml
if (i == )//第一頁不顯示上一頁strTable = strTableReplace($upUrl )
if (i <= )//上一頁分頁strTable = strTableReplace($upUrl <a + ……/ + strDate + shtml + >上一頁</a>)else……{ int p = i strTable = strTableReplace($upUrl <a + ……/ + strDate + _ + p + shtml + >上一頁</a>)}
if(upbound==)//如果只有一頁則不顯示頁碼//strNumber=strTable = strTableReplace($Number )else strTable = strTableReplace($Number strNumber)//頁碼替換/**///////////////////////// if(i==upbound)//最後一頁不顯示下一頁strTable = strTableReplace($downUrl )
if (i != upbound )//下一頁分頁……{ int q = i + strTable = strTableReplace($downUrl <a + ……/ + strDate + _ + q + shtml + >下一頁</a>)} else……{ int j = upbound strTable = strTableReplace($downUrl <a + ……/ + strDate + _ + j + shtml + >下一頁</a>)}
strhtml = strhtmlReplace($Pager strTable)} //創建文件信息對象——FileInfo finfo = new FileInfo(FilePath)//以打開或者寫入的形式創建文件流using (FileStream fs = finfoOpenWrite())
……{ //根據上面創建的文件流創建寫數據流StreamWriter sw = new StreamWriter(fs SystemTextEncodingDefault)//把新的內容寫到創建的HTML頁面中swWriteLine(strhtml)swFlush()swClose()} catch (Exception err)
……{ //輸出異常信息ResponseWrite(errToString())}
請不要直接拷貝使用裡面的路徑需要更改但程序絕對沒問題在我本地已經測試通過
From:http://tw.wingwit.com/Article/program/net/201311/13760.html