您的位置:首页 > 其它

.NET生成静态页面并分页 - 死亡凋零的专栏 - CSDNBlog

2008-03-27 15:59 302 查看
导读:
  
  因为公司的产品用asp开发, 前一段时间用asp写了一个生成静态页面并分页的程序,但缘于对.net的热爱,写了这个.net下的生成静态页面并分页的程序。
  主要的原理就是替换模板里的特殊字符。
  1、静态模板页面 template.html,主要是定义了一些特殊字符,用来被替换。
  $Title $Title
浏览次 $Time $Content $Pager



查看更多评论
  
  2、前态页面 NewsAdd.aspx,就是一个表单,用来填写新闻的标题和内容。
  <%@ Page Language="C#" AutoEventWireup="false" validateRequest="false" CodeFile="NewsAdd.aspx.cs" Inherits="NewsAdd.Admin_AdminPanel_NewsAdd" %><%@ Register TagPrefix="FCKeditorV2" Namespace="FredCK.FCKeditorV2" Assembly="FredCK.FCKeditorV2" %> 添加新闻

  
  3、后台页面 NewsAdd.aspx.cs
  using System;using System.Data;using System.Configuration;using System.Collections;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 Mysqlserver;using System.IO;using System.Text;namespace NewsAdd{
  public partial class Admin_AdminPanel_NewsAdd : System.Web.UI.Page
  {
  protected void Page_Load(object sender, EventArgs e)
  {
  } protected void Button1_Click(object sender, EventArgs e)
  {
  string strDate = DateTime.Now.ToString("yyMMdd") + "/" + DateTime.Now.ToString("yyyymmddhhmmss");
  string strFileName = strDate + ".shtml"//存储到数据库中 string strTitle=Request.Form["Title"].ToString().Trim();//接收传过来的标题 string strContent=Request.Form["Content"].ToString().Trim();//接收传过来的内容 string[] content = strContent.Split(new Char[] {'|'});//对内容进行拆分,并保存到数组 int upbound = content.Length;//数组的上限 SqlServerDataBase db = new SqlServerDataBase();
  bool success = db.Insert("insert into inNews(Title,Content,FilePath)values('" + strTitle + "','" + strContent + "','" + strFileName + "')", null);
  //if (success)
  // Message.Text = "添加成功!"; ///////////////////////////创建当前日期的文件夹开始
  string dir = Server.MapPath("../../"+"NewsFiles/"+DateTime.Now.ToString("yyMMdd"));//用来生成文件夹 if (!Directory.Exists(dir))
  {
  Directory.CreateDirectory(dir);
  } ///////////////////////////创建当前日期的文件夹结束
  try {
  for (int i = 0 i < content.Length; i++)
  {
  //string[] newContent = new string[4];//定义和html标记数目一致的数组 StringBuilder strhtml = new StringBuilder();
  //创建StreamReader对象 using (StreamReader sr = new StreamReader(Server.MapPath("../../" + "NewsFiles/") + "/template.html",Encoding.GetEncoding("gb2312")))
  {
  String oneline;
  //读取指定的HTML文件模板 while ((oneline = sr.ReadLine()) != null)
  {
  strhtml.Append(oneline);
  } sr.Close();
  } //为标记数组赋值
  //SqlServerDataBase db = new SqlServerDataBase(); DataSet ds = db.Select("select top 1 NewsId from inNews order by NewsId desc", null);//获取id string strTable = "? tr>< td>< td>
$downUrl< td>$Number<>  //这三个是用来替换的。 string FilePath="" strhtml = strhtml.Replace("$Title", strTitle);
  strhtml = strhtml.Replace("$NewsId", ds.Tables[0].Rows[0]["NewsId"].ToString());
  strhtml = strhtml.Replace("$Time", DateTime.Now.ToString("yyyy/MM/dd"));
  strhtml = strhtml.Replace("$Content", content[i]);
  string strNumber = ""//数字分页1,2,3…… for (int m = 1 m <=upbound; m++)
  {
  if (m == 1)//如果是第一页就显示成这个样子:20070524.shtml而不是20070524_1.shtml strNumber = strNumber + " ["+"?+?] ?< + m ?>? ?.shtml? strDate ..>  } else//否则按20070524.shtml、20070524_1.shtml 这种效果保存 {
  if (i == 0)
  FilePath = Server.MapPath("../../") + "NewsFiles" + "//" + strDate + ".shtml" else FilePath = Server.MapPath("../../") + "NewsFiles" + "//" + strDate + "_" + i + ".shtml"
  if (i == 0)//第一页不显示上一页 strTable = strTable.Replace("$upUrl", "");
  if (i <= 1)//上一页分页 strTable = strTable.Replace("$upUrl", "上一页<>  else {
  int p = i - 1 strTable = strTable.Replace("$upUrl", "上一页
");
  }
  if(upbound==1)//如果只有一页,则不显示页码
  //strNumber=""; strTable = strTable.Replace("$Number", "");
  else strTable = strTable.Replace("$Number", strNumber);//页码替换 ////////////////////////
  if(i==upbound-1)//最后一页不显示下一页 strTable = strTable.Replace("$downUrl", "");
  if (i != upbound - 1)//下一页分页 {
  int q = i + 1 strTable = strTable.Replace("$downUrl", "下一页");
  } else {
  int j = upbound - 1 strTable = strTable.Replace("$downUrl", "下一页");
  } strhtml = strhtml.Replace("$Pager", strTable);
  } //创建文件信息对象-------------------------------------------- FileInfo finfo = new FileInfo(FilePath);
  //以打开或者写入的形式创建文件流 using (FileStream fs = finfo.OpenWrite())
  {
  //根据上面创建的文件流创建写数据流 StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
  //把新的内容写到创建的HTML页面中 sw.WriteLine(strhtml);
  sw.Flush();
  sw.Close();
  } } } catch (Exception err)
  {
  //输出异常信息 Response.Write(err.ToString());
  } }}
  


  }
  
  请不要直接拷贝使用,里面的路径需要更改,但程序绝对没问题,在我本地已经测试通过。
  另外在使用时,比如我要把新闻的内容分成4页,就应该这样写:111|222|333|444。
  Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1624491
  收藏到我的网摘发送Trackback]评论
  #PM520 发表于2007-06-02 01:52:53 IP: 59.50.162.*
  和asp那篇一样,效率差
  #zdyguilong 发表于2007-06-02 08:44:10 IP: 60.28.62.*
  不知道您有什么好的解决办法
  #superghy 发表于2007-06-02 10:57:15 IP: 220.173.66.*
  能否把它打包放上来呀>?
  #tiantian1980 发表于2007-06-05 10:04:14 IP: 124.243.207.*
  也不说一下, 是VS2005的
  #xuu27 发表于2007-06-07 10:02:34 IP: 59.41.40.*
  读取模板文件可以改为sington方式,"cache"起来,这样后期的编辑就不用在一次io操作了
  #Terry001 发表于2007-06-10 00:05:24 IP: 222.210.176.*
  你应该把东西发全吧,那个用户控件怎么不发上来,还有你的数据库的表都有那些字段应该写出来吧,你这样发代码并让别人来你博客看这篇文章,就应该对朋友负责吧,服了你呢!!!
  #zdyguilong 发表于2007-06-10 13:37:42 IP: 221.239.62.*
  回复上面两位网友:
  即便是我把所有的代码包括数据库字段等都整理上来,你也不能直接用到你的项目上吧?再说那些字段在这句话中“insert into inNews(Title,Content,FilePath)values('" + strTitle + "','" + strContent + "','" + strFileName + "'”也能分析出来啊,难道连这点耐心都没有?那个用户控件就是fckeditor,就是类似于一个文本框。动动脑筋就ok的问题
  #fangyifeng 发表于2007-06-15 12:08:30 IP: 61.140.103.*
  分页那里使用无效果
  #ydmoment 发表于2007-07-14 16:33:14 IP: 125.121.225.*
  我现在项目中急需静态页面转换
  你的代码我能看懂
  能把源码给我一份么
  我的QQ是344314536
  在此先谢谢了
  #nextsun 发表于2007-07-30 18:24:26 IP: 125.95.11.*
  很有参考价值,收藏了~~~
  #jsxxf 发表于2007-08-03 17:21:39 IP: 222.45.52.*
  1,说人家写的效率差,那好的解决办法呢?
  2,要人家把所有东西都贴上来,你不过是想拿过去改改就可以用了是吧? 干脆数据库,什么整个的都给你算了~ 把这里当免费代码下载站了?
  有个方法参考是件好事~
  baidu+google的日子你还不过么?
  #zdyguilong 发表于2007-08-03 18:23:25 IP: 125.38.22.*
  还是楼上的朋友理解我……,呵呵
  #gzw323 发表于2007-08-09 19:40:45 IP: 220.205.60.*
  楼主 我用了你的代码,改了以下,实现了很多功能,呵呵 还有些问题,有时间加我QQ:43909413 我们好好交流以下
  #zdyguilong 发表于2007-08-17 17:26:58 IP: 125.38.22.*
  ls的朋友,在公司里不让用QQ,不好意思,可以发邮件给我
  zdyguilong@163.com
  #zshong512 发表于2007-08-21 08:36:50 IP: 121.204.182.*
  支持jsxxf的话~做这行的.靠的就是自学~实在不懂才求助人.
  #zzl280382128 发表于2007-08-25 10:56:42 IP: 121.228.33.*
  确实啊...这行..很多事情..都是不好说的...
  #yangzhijia 发表于2007-09-04 17:00:20 IP: 219.232.38.*
  静态页面是由客户端生成还是由服务器端生成的.
  如果是由客户端生成,那怎么判断它已经生成了一个静态页面 而不用再次生成?
  #nbgcqlk 发表于2007-09-27 18:07:40 IP: 125.115.63.*
  又长了知识..如果模板大点的话,而且分的页数太多,不知道会怎样..
  #xz_xq 发表于2007-10-10 14:56:11 IP: 202.152.186.*
  支持楼主
  #kkshizhu520 发表于2007-10-26 16:37:20 IP: 222.137.237.*
  支持,做个简单的程序找地方传上来吧,大家分享一下~期待?!ing...
  #zdyguilong 发表于2007-11-02 10:09:56 IP: 125.38.24.*
  谢谢ls的这么多兄弟
  在前一段时间确实打算写一个小项目来体现生成静态页面并分页,但由于工作问题而搁浅
  但过段时间等工作忙的差不多了,我肯定会写一个小项目发上来
  再次谢谢朋友们的支持
  #liuterry 发表于2007-12-10 11:30:43 IP: 61.130.6.*
  值得学习下!谢谢楼主
  发表评论
  大名:
  网址:
  校验码:
  评论
  
  
  当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录

本文转自
http://blog.csdn.net/zdyguilong/archive/2007/05/24/1624491.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: