1、从数据库中获取新闻内容。包括新闻标题、内容等。
2、设置页面显示的字符串长度,获取新闻内容的字符长度。做初始设置
3、按页面大小设置的长度截取新闻内容的字符。
4、获取模板页面。在相关的替换字符中用标题、内容等替换字符串。
5、保存新页面到制定目录下面
下面是代码和资源管理器内容、
solid.aspx内容
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.IO;
publicpartialclasssolid:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
WriteFile("奥运专题","奥运会真棒!<br/>","2","2.html");
}
publicvoidWriteFile(stringstrText,stringcontent,stringpage,stringname)
{
stringpath=HttpContext.Current.Server.MapPath("news/get");
System.Text.Encodingcode=System.Text.Encoding.GetEncoding("gb2312");
//读取模板文件
stringtemp=HttpContext.Current.Server.MapPath("news/text.htm");
StreamReadersr=null;
StreamWritersw=null;
stringstr="";
try
{
sr=newStreamReader(temp,code);
str=sr.ReadToEnd();//读取文件
}
catch(Exceptionexp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
stringhtmlfilename=name.ToString();
//替换内容
//这时,模板文件已经读入到名称为str的变量中了
str=str.Replace("$title",strText);//模板页中的$title,即标题
str=str.Replace("$content",content);//模板页中的$content,即内容
str=str.Replace("$page",page);//模板页中的$page,即页码连接格式
//写文件
try
{
sw=newStreamWriter(path+"/"+htmlfilename,false,code);
sw.Write(str);
sw.Flush();
}
catch(Exceptionex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
}
}
text.htm内容
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<title>无标题页</title>
</head>
<body>
<divstyle="font-size:20px;text-align:center;">文章分页测试</div>
<divstyle="font-size:15px;text-align:center;color:#009966;">$title</div>
<divstyle="font-size:12px;text-align:center;color:darkgray;">$page</div>
<divstyle="font-size:12px;">$content</div>
</body>
</html>