asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

ASP.NET生成静态页面和分页主要的原理


发布日期:2020年08月28日
 
ASP.NET生成静态页面和分页主要的原理

静态模板页面 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(yyyymmddhhmmssstring 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())}

请不要直接拷贝使用里面的路径需要更改但程序绝对没问题在我本地已经测试通过

               

上一篇:提高ASP.Net应用程序性能的十大方法

下一篇:ASP.NET中的DataGrid的属性