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

Reading number is top 10 articles
asp.net点击textbox自动清除其中文字_[Asp.Net教程]
Sql,Server2005实现远程备份数据库_mssql学习_编程技术
IsPostBack深入探讨_[Asp.Net教程]
ASP.NET用Repeater控件显示数据_[Asp.Net教程]
向上循环滚动带停留的js特效_JavaScript技术_编程技术
SQL SERVER 和EXCEL的数据导入导出_[SQL Server教程]
datagrid导出excel文件给客户端下载的3种方法_[Asp.Net教程]
ASP.NET自动识别GB2312与UTF-8编码的文件_.net资料_编程技术
PHP程序指定目录里的指定后缀名文件为超连接_[PHP教程]
实例讲解动态网页技术PHP开发文件系统_php资料_编程技术
Reading number is top 10 pictures
A man's favorite things10
Beauty ZhiHuiLin1
红楼梦金陵十二钗(2)
Small s breast enhancement demonstration
Absolutely shocked. National geographic 50 animal photographys10
如果我是导演...
人美胸美腿更美2
湖南中医院的养生八图
囚犯暴乱了咋办?
美女
Download software ranking
The cock of the Grosvenor LTD handsome
Unix video tutorial9
c#程序设计案例教程
实战黑客不求人
好色的外科大夫
Unix video tutorial14
Kung.Fu.Panda.2
Boxer vs Yellow3
Twenty piece of palm leaf
Sora aoi's film--cangkong_Blue.Sky
delv published in(发表于) 2014/1/27 6:48:46 Edit(编辑)
Asp.net生成静态网页的实现代码_[Asp.Net教程]

Asp.net生成静态网页的实现代码_[Asp.Net教程]

Asp.net生成静态网页的实现代码_[Asp.Net教程]

  现在做程序都要将动态的页面转换成静态页面,今天教大家在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.