欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Asp.Net 生成静态页并实现分页效果

程序员文章站 2024-03-08 15:23:58
1、静态模板页面 template.html,主要是定义了一些特殊字符,用来被替换。 复制代码 代码如下:
1、静态模板页面 template.html,主要是定义了一些特殊字符,用来被替换。
复制代码 代码如下:

<!doctype html public "-//w3c//dtd html 4.01 transitional//en""http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>title</title>
</head>
<body>
<div style="width: 417px; height: 54px" align="center">
<br />
title</div>
<div style="width: 417px; height: 8px">
浏览<font color="red"><script src="http://localhost/.net/newsfiles/clickcount.aspx?newsid=newsid"></script></font>次 time</div>
<div style="width: 417px; height: 100px">
content</div>
<div style="width: 416px; height: 9px">
pager</div>
<div style="width: 416px; height: 8px">
<form id="form1" action="../addcomment.aspx" style="margin:0px">
<input id="text1" type="text" /><img id="image1" src="http://www.dwww.cn/userinfo/checkcode.aspx"/><br />
<textarea id="commentcontent" cols="20" rows="2"></textarea>
<br />
<input id="newsid" type="hidden" value="newsid"/>
<input id="button1" type="submit" value="button" />
<a href="http://www.dwww.cn/news/display.aspx?newsid=newsid">查看更多评论</a></form>
</div>
</body>
</html>


2、前态页面 newsadd.aspx,就是一个表单,用来填写新闻的标题和内容。
复制代码 代码如下:

<%@ page language="c#" validaterequest="false" codefile="newsadd.aspx.cs" inherits="newsadd.admin_adminpanel_newsadd" %>
<%@ register tagprefix="fckeditorv2" namespace="fredck.fckeditorv2" assembly="fredck.fckeditorv2" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>添加新闻</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:label id="label2" runat="server" text="标题"></asp:label>
<asp:textbox id="title" runat="server" width="325px"></asp:textbox><br />
<asp:label id="label1" runat="server" text="内容"></asp:label>
<fckeditorv2:fckeditor id="content" basepath="~/fckeditor/" runat="server" height="400px" width="70%"></fckeditorv2:fckeditor>
<asp:button id="button1" runat="server" onclick="button1_click" text="button" />
<asp:label id="message" runat="server" ></asp:label></div>
</form>
</body>
</html>



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.cnblogs.com/"+"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.cnblogs.com/" + "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 = "<table><tr><td>upurl</td><td>number</td><td>downurl</td></tr></table>";//上下页表格,注意此处的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 <=upbound; m++)
{
if (m == 1)//如果是第一页就显示成这个样子:20070524.shtml而不是20070524_1.shtml
strnumber = strnumber + " ["+"<a href=" + "../" + strdate + ".shtml" + ">" + m + "</a>"+"] ";
else
{
int n = m - 1;//第三页的连接应该是20070524_2.shtml,以此类推
strnumber = strnumber + " [" +"<a href=" + "../" + strdate + "_" + n + ".shtml" + ">" + m + "</a>"+"] ";
}
}
if (upbound == 0)//如果没有分页,就直接按日期时间保存
{
filepath = server.mappath("http://www.cnblogs.com/") + "newsfiles" + "//" + strdate + ".shtml";
strhtml = strhtml.replace("pager", "");
}
else//否则按20070524.shtml、20070524_1.shtml 这种效果保存
{
if (i == 0)
filepath = server.mappath("http://www.cnblogs.com/") + "newsfiles" + "//" + strdate + ".shtml";
else
filepath = server.mappath("http://www.cnblogs.com/") + "newsfiles" + "//" + strdate + "_" + i + ".shtml";

if (i == 0)//第一页不显示上一页
strtable = strtable.replace("upurl", "");

if (i <= 1)//上一页分页
strtable = strtable.replace("upurl", "<a href=" + "../" + strdate + ".shtml" + ">上一页</a>");
else
{
int p = i - 1;
strtable = strtable.replace("upurl", "<a href=" + "../" + strdate + "_" + p + ".shtml" + ">上一页</a>");
}

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", "<a href=" + "../" + strdate + "_" + q + ".shtml" + ">下一页</a>");
}
else
{
int j = upbound - 1;
strtable = strtable.replace("downurl", "<a href=" + "../" + strdate + "_" + j + ".shtml" + ">下一页</a>");
}

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());
}
}
}
}