asp.net生成静态页面并分页的实现方法
本文导语: 1、静态模板页面 template.html 定义模板标签,即一些特殊字符,在程序中被替换。 代码示例: $Title $Title 浏览次 $Time $Content $Pager ...
1、静态模板页面 template.html
定义模板标签,即一些特殊字符,在程序中被替换。
2、前态页面 NewsAdd.aspx
其实就是一个表单,用来填写新闻的标题和内容。
添加新闻
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("http://www./"+"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("http://www./" + "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 = "$upUrl$Number$downUrl";//上下页表格,注意此处的$upUrl(上一页),$Number(页码分页),$downUrl(下一页)
//这三个是用来替换的。
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