日期:2014-05-20  浏览次数:20790 次

动态页面生成静态页面的方法
在动态页面上有一个按钮,我想点击该按钮时(比如该按钮的名称为:生成静态页面)将动态页面生成静态页面,想求各位高手能给我详细的代码,以及事件代码。初步的代码我有 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;
  }

------解决方案--------------------
你核心代码都有了,其他代码还不会写 ?其他要读取那些模板,标签怎么定义这些都是需要你自己设计的吧 ,别人怎么给源码给你,或者你去 51aspx上面下载一些开源的源码,看看
------解决方案--------------------
asp.net 的缓存就可以啦