动态页面生成静态页面的方法

IT0329 2011-03-14 03:07:57
在动态页面上有一个按钮,我想点击该按钮时(比如该按钮的名称为:生成静态页面)将动态页面生成静态页面,想求各位高手能给我详细的代码,以及事件代码。初步的代码我有 protected void gvnews_RowCommand(object sender, GridViewCommandEventArgs e)
{
string conStr = "Server=(local);DataBase=ZhongXiangDB.mdb;Uid=admin;Pwd=hzzx123456";
string cmdtxt = "select * from newsinfo";
OleDbConnection conn = new OleDbConnection(conStr);
OleDbCommand cmd = new OleDbCommand(cmdtxt, conn);
OleDbDataReader dr = cmd.ExecuteReader();

if (dr.HasRows)
{
WriteFile(dr["newstitle"].ToString(), dr["newssource"].ToString(), dr["newscontent"].ToString());
Response.Write("<script>alert('静态页生成成功!');location='Default.aspx'</script>");
}
dr.Close();
}

public bool WriteFile(string strText, string newssource, string newscontent)
{
string OutPutPath = HttpContext.Current.Server.MapPath("/news/");
Encoding encoding = Encoding.GetEncoding("gb2312");
// 读取模板文件
string ModelTemp = HttpContext.Current.Server.MapPath("/news/text.html");
StreamReader sr = null;
StreamWriter sw = null;
string str = "";
try
{
sr = new StreamReader(ModelTemp, encoding);
str = sr.ReadToEnd(); // 读取文件
}
catch (Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}


string HtmlFilename = DateTime.Now.ToString("yyyyMMddHHmmss_") + ".html";
// 替换内容
str = str.Replace("ShowPageTitle", strText);
str = str.Replace("newstitle", strText); //模板页中的PageArticle
str = str.Replace("newssource", newssource);
str = str.Replace("newscontent", newscontent);
// 写文件
try
{
sw = new StreamWriter(OutPutPath + HtmlFilename, false, encoding);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;
}
...全文
95 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
IT0329 2011-03-15
  • 打赏
  • 举报
回复
问题搞定,谢了!
jianshao810 2011-03-15
  • 打赏
  • 举报
回复
asp.net 的缓存就可以啦
yidongliange4 2011-03-15
  • 打赏
  • 举报
回复
你核心代码都有了,其他代码还不会写 ?其他要读取那些模板,标签怎么定义这些都是需要你自己设计的吧 ,别人怎么给源码给你,或者你去 51aspx上面下载一些开源的源码,看看
IT0329 2011-03-15
  • 打赏
  • 举报
回复
一个CSDN这么大的论坛里没有一个人会这道题么?

13,347

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET技术前瞻
社区管理员
  • .NET技术前瞻社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧