.NET生成静态页面并分页 - 死亡凋零的专栏 - CSDNBlog
来源:互联网 发布:网络固定电话号码申请 编辑:程序博客网 时间:2024/04/28 04:50
导读:
因为公司的产品用asp开发, 前一段时间用asp写了一个生成静态页面并分页的程序,但缘于对.net的热爱,写了这个.net下的生成静态页面并分页的程序。
主要的原理就是替换模板里的特殊字符。
1、静态模板页面 template.html,主要是定义了一些特殊字符,用来被替换。
$Title
$Title
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 = "
因为公司的产品用asp开发, 前一段时间用asp写了一个生成静态页面并分页的程序,但缘于对.net的热爱,写了这个.net下的生成静态页面并分页的程序。
主要的原理就是替换模板里的特殊字符。
1、静态模板页面 template.html,主要是定义了一些特殊字符,用来被替换。
$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 = "