All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
浅析SQL,Server全文检索查询_mssql学习_编程技术
asp.net点击textbox自动清除其中文字_[Asp.Net教程]
SQL,Server,2005,Reporting,Services,初次使用感受_[SQL,Server教程]
GridView,实现服务器端和客户端全选的两种方法_[Asp.Net教程]
Ajax程序中,自己实现页面前进、后退、与标签功能
教你轻松给流程增加权限控制程序_.net资料_编程技术
ASP.NET技巧:开发时复制aspx网页的方法_[Asp.Net教程]
GridView实用示例源码(可以导出Excel)_[Asp.Net教程]
打开delphi对话框组件(TOpenDialog)实例
SQL Server视图使用中4个限制条件_[SQL Server教程]
Reading number is top 10 pictures
9.3阅兵全景图6-常规导弹梯队和核导弹梯队
史上最大的哺乳动物迁移
Absolutely shocked. National geographic 50 animal photographys3
看如何给单纯的少女洗脑
Sora aoi in China3
Sora aoi on twitter4
全身蕾丝丝质美臀
Take you to walk into the most true north Korea rural4
Thrilling English baby
Summer is most suitable for young people to travel in China7
Download software ranking
Unix video tutorial2
VeryCD电驴(EasyMule) V1.1.9 Build09081
Be there or be square
Boxer's Top ten classic battle10
Boxer's Top ten classic battle9
Tram sex maniac 2 (H) rar bag8
Popkart Cracked versions Mobile phone games
传奇私服架设教程-chm
Visual C++界面编程技术
尖东毒玫瑰B
aaa published in(发表于) 2013/12/18 7:56:37 Edit(编辑)
Asp.net生成静态网页的实现代码_.net资料_编程技术

Asp.net生成静态网页的实现代码_.net资料_编程技术

Asp.net生成静态网页的实现代码_.net资料_编程技术-你的首页-uuhomepage.com

  现在做程序都要将动态的页面转换成静态页面,今天教大家在ASP.NET 中实现静态页面的生成方法。


using System;
  using System.Data;
  using System.Configuration;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Web.UI.HtmlControls;
  using System.IO;
  using System.Text;
  ///
  /// WriteFile 的摘要说明
  ///
  public class WriteFile
  {
  public WriteFile()
  {
  }
  public static bool createHtml(string[] strnewsHtml,string[] stroldHtml,string strModeFilePath,string strPath)
  {
  bool flag = false;
  StreamReader sr = null;
  StreamWriter sw = null;
  string filepath = HttpContext.Current.Server.MapPath(strModeFilePath);
  Encoding code = Encoding.GetEncoding("gb2312");
  string s = string.Empty;
  try
  {
  sr = new StreamReader(filepath,code);
  s = sr.ReadToEnd();
  }
  catch (Exception ex)
  {
  throw ex;
  }
  finally
  {
  sr.Close();
  }
  try
  {
  for (int i = 0; i < strnewsHtml.Length; i++)
  {
  s = s.Replace(stroldHtml[i], strnewsHtml[i]);
  }
  sw = new StreamWriter(HttpContext.Current.Server.MapPath(strPath), false, code);
  sw.Write(s);
  flag = true;
  }
  catch (Exception ex)
  {
  flag = false;
  throw ex;
  }
  finally
  {
  sw.Flush();
  sw.Close();
  }
  return flag;
  }
  public static bool UpdateHtmlPage(string[] strNewsHtml, string[] strStartHtml, string[] strEndHtml, string strHtml)
  {
  bool Flage = false;
  StreamReader ReaderFile = null;
  StreamWriter WrirteFile = null;
  string FilePath = HttpContext.Current.Server.MapPath(strHtml);
  Encoding Code = Encoding.GetEncoding("gb2312");
  string strFile = string.Empty;
  try
  {
  ReaderFile = new StreamReader(FilePath, Code);
  strFile = ReaderFile.ReadToEnd();
  }
  catch (Exception ex)
  {
  throw ex;
  }
  finally
  {
  ReaderFile.Close();
  }
  try
  {
  int intLengTh = strNewsHtml.Length;
  for (int i = 0; i < intLengTh; i++)
  {
  int intStart = strFile.IndexOf(strStartHtml[i]) + strStartHtml[i].Length;
  int intEnd = strFile.IndexOf(strEndHtml[i]);
  string strOldHtml = strFile.Substring(intStart, intEnd - intStart);
  strFile = strFile.Replace(strOldHtml, strNewsHtml[i]);
  }
  WrirteFile = new StreamWriter(FilePath, false, Code);
  WrirteFile.Write(strFile);
  Flage = true;
  }
  catch (Exception ex)
  {
  throw ex;
  }
  finally
  {
  WrirteFile.Flush();
  WrirteFile.Close();
  }
  return Flage;
  }
  }
  调用公共类:
  ----------------------------------------------------------------------------
  protected void Button2_Click(object sender, EventArgs e)
  {
  string NewsTitle = this.TextBox1.Text;
  string NewsKindName = this.DropDownList1.SelectedItem.Text;
  string NewsBody = this.WebEditor1.Text;
  DateTime PubTime = DateTime.Now;
  string UserName = Session["UserName"].ToString();
  Response.Write(NewsKindName);
  string[] strNewsHtml = new string[] { NewsTitle, NewsKindName, NewsBody, PubTime.ToString(), UserName };
  string[] strOldHtml = new string[] { "@Title", "@NewsKInd", "@NewsBody", "@PubTime", "@UserName" };
  string strFileName = DateTime.Now.ToString("ddhhmmss") + ".html";
  string strFilePath = string.Format("NewsHtml/{0}", strFileName);
  try
  {
  if (WriteFile.createHtml(strNewsHtml, strOldHtml, "mode.htm", strFilePath))
  {
  this.Label1.Text = "生成成功!";
  }
  else
  {
  this.Label1.Text = "生成失败!";
  }
  }
  catch
  {
  this.Label1.Text = "生成失败!";
  }
  }
  protected void Button3_Click(object sender, EventArgs e)
  {
  string[] strNewsHtml=new string[]{"到此一游!"};
  string[] strStartHtml=new string[]{""};
  string[] strEndHtml=new string[]{""};
  if (WriteFile.UpdateHtmlPage(strNewsHtml, strStartHtml, strEndHtml, "NewsHtml/02011139.html"))
  {
  this.Label1.Text="生成首页成功!";
  }
  else
  {
  this.Label1.Text="生成首页失败!";
  }
  }


  新建文件夹NewsHtml,生成html文件放在里面


  -----------------------------------------------------------


  增加一个模板文件




无标题页








@Title







发布人:@UserName &nbsp; &nbsp; 发布时间:@PubTime &nbsp; &nbsp;&nbsp; 新闻类别:@NewsKInd







@NewsBody










添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.