新闻系统中为了减轻服务器的负担。有时需要把数据库内容不是动态读出,
而是写成静态的网页。

以下是写html。更新html,和删除html的代码。
工程中Conn.cs是公共调用的一个类库。(看梦凌早期写的论坛Access版学来的。(转)在HTML页面中实现点击数统计 For ASP.Net版! (接近正解)

//生成HTML页
 
         public static bool WriteFile(string strText,string strContent,string strAuthor,string filename)
         {
              string yy=DateTime.Now.ToString("yyyy");
              string dd=DateTime.Now.ToString("dd");
              string mm=DateTime.Now.ToString("MM");
              string path = HttpContext.Current.Server.MapPath("/news/"+yy.ToString()+"/"+mm.ToString()+"/"+dd.ToString()+"/");
    
              string addtime=DateTime.Now.ToString();
              Encoding code = Encoding.GetEncoding("gb2312");
              // 读取模板文件
              string temp = HttpContext.Current.Server.MapPath("/news/template/template.html");
              StreamReader sr=null;
              StreamWriter sw=null;
              string str=""; 
              try
              {
                   sr = new StreamReader(temp, code);
                   str = sr.ReadToEnd(); // 读取文件
              }
              catch(Exception exp)
              {
                   HttpContext.Current.Response.Write(exp.Message);
                   HttpContext.Current.Response.End();
                   sr.Close();
              } 
  
              string htmlfilename=DateTime.Now.ToString("HHmmss")+".html";
             
              // 替换内容
              // 这时,模板文件已经读入到名称为str的变量中了
              str = str.Replace("$Title$",strText);
              str = str.Replace("$filename$",filename);
              str = str.Replace("$Content$",strContent);
              str = str.Replace("$Author$",strAuthor);
              str = str.Replace("$AddTime$",addtime);
             
 
              // 写文件
              if(!System.IO.Directory.Exists(path))
              {
                   System.IO.Directory.CreateDirectory(path);
                   try
                   {
                       sw = new StreamWriter(path + htmlfilename , false, code);
                       sw.Write(str);
                       sw.Flush();
                   }
                   catch(Exception ex)
                   {
                       HttpContext.Current.Response.Write(ex.Message);
                       HttpContext.Current.Response.End();
                   }
                   finally
                   {
                       sw.Close();
                   }
                   return true;
              }
              else
              {
                   try
                   {
                       sw = new StreamWriter(path + htmlfilename , false, code);
                       sw.Write(str);
                       sw.Flush();
                   }
                   catch(Exception ex)
                   {
                       HttpContext.Current.Response.Write(ex.Message);
                       HttpContext.Current.Response.End();
                   }
                   finally
                   {
                       sw.Close();
                       sr.Close();
                   }
                   return true;
              }
         }
         //修改HTML页
         public static bool ModifyFile(string strText,string strContent,string strAuthor,string htmlfilename)
         {
        
              Encoding code = Encoding.GetEncoding("gb2312");
              string path=HttpContext.Current.Server.MapPath("/news/"+htmlfilename);
              // 读取模板文件
              string temp = HttpContext.Current.Server.MapPath("/news/template/template.html");
              StreamReader sr=null;
              StreamWriter sw=null;
              string str="";        
              try
              {
                   sr = new StreamReader(temp, code);
                   str = sr.ReadToEnd(); // 读取文件
              }
              catch(Exception exp)
              {
                   HttpContext.Current.Response.Write(exp.Message);
                   HttpContext.Current.Response.End();
                   sr.Close();
              }
        
        
              string addtime=DateTime.Now.ToString();
              // 替换内容
              // 这时,模板文件已经读入到名称为str的变量中了
              str = str.Replace("$Title$",strText);
              str = str.Replace("$Content$",strContent);
              str = str.Replace("$Author$",strAuthor);
              str = str.Replace("$AddTime$",addtime);
              // 写文件
              try
              {
                   sw = new StreamWriter(path , false, code);
                   sw.Write(str);
                   sw.Flush();
              }
              catch(Exception ex)
              {
                   HttpContext.Current.Response.Write(ex.Message);
                   HttpContext.Current.Response.End();
              }
              finally
              {
                   sw.Close();
                   sr.Close();
              }
              return true;
         }
        
         //删降HTML        
         public static void Delete(string htmlfilename)
         {
              string path = HttpContext.Current.Server.MapPath("/news/"+htmlfilename);
              try
              {                 
                   File.Delete(path);
              }
              catch (Exception ex)
              {
                   HttpContext.Current.Response.Write(ex.Message);
              }
 
         }
关于生成html页请看 http://www.cnblogs.com/hover/archive/2004/04/30/8191.aspx
以下是counter.aspx的代码。

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
 
namespace Hover.Article
{
     ///<summary>
     /// Counter 的摘要说明。
     ///</summary>
     public class Counter : System.Web.UI.Page
     {
         protected Hover.Conn obj=new Hover.Conn();
 
         public DataRow dr;
 
         public String filename;
 
         private void Page_Load(object sender, System.EventArgs e)
         {
              // 在此处放置用户代码以初始化页面
              filename=Request.Params["filename"];   
              if (!this.Page.IsPostBack)
              {
                   if (filename!=null)
                   {            
                       UpdateClick(filename);
 
                   }
                   else
                   {
                       Response.Write ("无参数传递错误!");
                   }     
                   this.ReadClick(filename);
              }
 
         }
 
         public void UpdateClick(string filename)
 
         {
 
              try
 
              {
 
                   OleDbDataReader rs=null;
 
                   obj.open1();
 
                   rs=obj.ccmd("select click from news where filename='"+filename+"'").ExecuteReader();
 
                   rs.Read();
 
                   int i = rs.GetInt32(0);
 
                   i++;
 
                   rs.Close();
 
                   obj.ccmd("UPDATE news SET click = "+i.ToString()+" WHERE filename='"+filename+"'").ExecuteNonQuery();
 
                   obj.link.Close();
 
              }
 
              catch(Exception ex)
 
              {
 
                   Response.Write(2+ex.Message );
 
              }
 
         }
 
 
         #region Web 窗体设计器生成的代码
         override protected void OnInit(EventArgs e)
         {
              //
              // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
              //
              InitializeComponent();
              base.OnInit(e);
         }
        
         ///<summary>
         ///设计器支持所需的方法 - 不要使用代码编辑器修改
         ///此方法的内容。
         ///</summary>
         private void InitializeComponent()
         {   
              this.Load += new System.EventHandler(this.Page_Load);
         }
         #endregion
 
         ///<summary>
         ///读click数
         ///</summary>
         public void ReadClick(string filename)
         {
              string Sql="select click from news where filename='"+filename+"'";
 
              try
 
              {
 
                   obj.open1();
 
                   OleDbDataAdapter da=new OleDbDataAdapter(Sql,obj.link);
 
                   DataSet ds=new DataSet();
 
                   da.Fill(ds,"RecordClick");
 
                   dr = ds.Tables["RecordClick"].Rows[0];
 
                   string filename1=dr["click"].ToString();
                    Response.Write("document.write('"+filename1.ToString()+"')") ;
             
                   obj.link.Close();
 
              }
 
              catch(Exception ex)
 
              {
 
                   Response.Write(ex.Message );
 
              }
 
 
         }
     }
}
 
注:不知道该不该写上版权所有。上面其中的代码。有很多是借鉴前人的代码。
只要大家不认为我是“盗版”就行。
http://cnblogs.com/hover/articles/177.aspx 
这个我是改成csharp版了算盗版吗。(转)在HTML页面中实现点击数统计 For ASP.Net版! (接近正解)
都是一大堆叙述。看的人不要烦哦。(转)在HTML页面中实现点击数统计 For ASP.Net版! (接近正解) 
这是实例:http://bns.23city.com/news/2004/05/16/191640.html
因为asp版的是传递新增加的ID。我这不是所以叫接近正解版!(转)在HTML页面中实现点击数统计 For ASP.Net版! (接近正解)
 

相关文章:

  • 2022-02-21
  • 2021-10-06
  • 2021-12-02
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-10
  • 2021-10-19
  • 2021-09-23
  • 2022-12-23
  • 2021-10-07
相关资源
相似解决方案