思路: 生成静态网页(更新静态网页内容)
1:> 读取静态网页 (已经存在一个静态网页模板)
2> 查找替换 : 将静态网页中要更新的内容替换成要更新的数据
3>写入值 : 重新将值写入
Default.aspx :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
动态网页转化成静态 Default.aspx.cs :
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (this.Page.IsPostBack)
return;
}
///
/// 转成静态网页
/// ///
///
protected void Button1_Click(object sender, EventArgs e)
{
replaceHtml(Convert.ToInt32(this.TextBox1.Text.Trim()));
}
///
/// 功能: 返回文件名称 所在物理路径
/// ///
///
返回一个绝对地址 public string physicsPath(string xmlPath)
{
try
{
return Server.MapPath(xmlPath);
}
catch (Exception)
{
throw;
}
}
///
/// 功能: 根据sql语句 返回数据集
/// ///
新闻表ID
///
数据集 public DataSet getDataSet(int num)
{
try
{
string sql = "select * from News where NewsID= " + num;
return SqlHelper.ExecuteDataset(SqlHelper.CONN_STRING_LC, CommandType.Text, sql);
}
catch (Exception)
{
throw;
}
}
///
/// 功能: 根据HTML文件 返回HTML字符串
/// ///
HTML所在路径
///
public string readHtml(string htmlPath)
{
try
{
System.IO.StreamReader sr = new System.IO.StreamReader(physicsPath(htmlPath), System.Text.Encoding.GetEncoding("gb2312"));
string _test = sr.ReadToEnd();
sr.Close();
return _test;
}
catch (Exception)
{
throw;
}
}
///
/// 功能: 根据新闻表的ID生成静态的新闻页(更新)
/// ///
public void replaceHtml(int num)
{
string htmlTest = readHtml("index.html");
DataSet ds = getDataSet(num);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
try
{
htmlTest = htmlTest.Replace("$我的主题$", ds.Tables[0].Rows[i]["Title"].ToString());
htmlTest = htmlTest.Replace("$我的内容$", ds.Tables[0].Rows[i]["Content"].ToString());
htmlTest = htmlTest.Replace("$我的作者$", ds.Tables[0].Rows[i]["Author"].ToString());
htmlTest = htmlTest.Replace("$我的时间$", ds.Tables[0].Rows[i]["PostedDate"].ToString());
htmlTest = htmlTest.Replace("$我的来源$", ds.Tables[0].Rows[i]["source"].ToString());
string path = Convert.ToDateTime(ds.Tables[0].Rows[i]["PostedDate"]).ToString("yyyyMMddHHmmss") + ".html";
writeHtml(physicsPath(path), htmlTest);
}
catch (Exception)
{
throw;
}
}
}
///
/// 功能: 刷新HTML(更新)
/// ///
///
public void writeHtml(string htmlPath,string html)
{
try
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(htmlPath, false, System.Text.Encoding.GetEncoding("gb2312"));
sw.Write(html);
sw.Flush();
sw.Close();
}
catch (Exception)
{
throw;
}
}
}
---------------------------------------------
1.在Global.asax文件中导入命名空间
<%@ Import Namespace="System.Timers" %>
2.Global.asax文件中的Application_Start()方法内写如下代码:
System.Timers.Timer objTimer = new Timer();
objTimer.Interval = 时间; //这个时间单位毫秒,比如10秒,就写10000
objTimer.Enabled = true;
objTimer.Elapsed += new ElapsedEventHandler(objTimer_Elapsed);
3.Global.asax文件中添加一个方法
void objTimer_Elapsed(object sender, ElapsedEventArgs e)
{
//这个方法内实现你想做的事情。
//例如:修改Application的某一个值等等。
Default d = new Default();
d.replaceHtml();
}