.NET生成静态页面并分页
因为公司的产品用asp开发, 前一段时间用asp写了一个生成静态页面并分页的程序,但缘于对.net的热爱,写了这个.net下的生成静态页面并分页的程序。
主要的原理就是替换模板里的特殊字符。
1、静态模板页面 template.html,主要是定义了一些特殊字符,用来被替换。
<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.cnblogs.com/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="../Display.aspx?NewsId=$NewsId">查看更多评论</a></form>
</div>
</body>
</html>
2、前态页面 NewsAdd.aspx,就是一个表单,用来填写新闻的标题和内容。
<%...@ 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.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());
}
}
}
}
请不要直接拷贝使用,里面的路径需要更改,但程序绝对没问题,在我本地已经测试通过。
另外在使用时,比如我要把新闻的内容分成4页,就应该这样写:111|222|333|444。
.NET生成静态页面并分页相关推荐
- Asp.NET生成静态页面并分页
因为公司的产品用asp开发, 前一段时间用asp写了一个生成静态页面并分页的程序,但缘于对.net的热爱,写了这个.net下的生成静态页面并分页的程序. 主要的原理就是替换模板里的特殊字符. 1.静态 ...
- [转]Asp.NET生成静态页面并分页
比较懒,项目上也没有用到,懒得研究了.今天逛CSDN无意看到有个火柴妞的贴子,就COPY过来了, 我也没仔细看,以后再看吧,算个参考. 原理:替换模板里的特殊字符.(卜:还有其它方法吗?) 1.静态模 ...
- php生成文章页,php结合smarty生成静态页面php文章内分页代码
本文介绍了php中使用smarty模板引擎生成静态页面的方法,以及php实现文章分页的实现代码,需要的朋友参考下. 例子,php中smarty生成静态页.文章内分页代码.专题:php分页代码原理 ', ...
- 关于ASP.NET给产品分类,分页,详情页生成静态页面
之前讲了如何给栏目页生成静态.现在剩下复杂的产品分类,分页,详情页生成静态页面. 我采用的原理是.产品分类通过循环全部生成静态页面. 这个就不说了,跟之前生成栏目页方法一样. 接下来是产品分页和详情页 ...
- ASP新闻分页,将一篇过长的文章分页,生成静态页面
老话题,新问题:新闻分页,将一篇过长的文章分页,生成静态页面. 问题来源于制作大型新闻文章管理系统,往往碰到文章超长的情况. 现在将他们生成静态页面没有什么问题,但是如何将它们按照某种规则,生成编号为 ...
- PHP代码为什么不能直接保存HTML文件——PHP生成静态页面教程
1.服务器会根据文件的后缀名去进行解析,如果是HTML文件则服务器不会进行语法解析,而是直接输出到浏览器. 2.如果一个页面中全部都是HTML代码而没有需要解析的PHP语法,则没有必要保存为PHP文件 ...
- php读取模板生成静态功能,php 生成静态页面的办法与实现代码详细版
php中主要用到的就是要用到fread()和fwirte().而静态页面生成了之后,就会牵扯到修改的问题.这里可以用到正则匹配的方法来替换模版中改变的部位.不过此种方法太麻烦,值得推荐的方法是直接把原 ...
- 网站生成静态页面,及网站数据采集的攻、防原理和策略
关于本文: 本着互联网共享主义精神,特写此文献给建站新手,授人以鱼,不如授人以渔,本文所讲的只是方法和原理,希望大家看完此文,能够从中得到些帮助和启发. 本文从一个ASP程序员的角度用 ...
- 动态网站如何生成静态页面
本文从一个ASP程序员的角度用大量实例祥细介绍了动态网站如何生成静态页面原理,又从一个采集程序开发者的角度去祥细介绍了网站数据采集的原理及功防策略. 关于网站生成静态页面,为了让本文读者容易理解,文内 ...
最新文章
- 尾递归对时间与空间复杂度的影响(上)
- Too Many Segments (hard version) CodeForces - 1249D2(贪心+容器vector+set)
- eclipse android 分包,eclipse中multidex的使用
- 记一次DRBD Unknown故障处理过程
- 一个好的算法工程应该具备哪几点要素?
- 实战爬虫:python爬虫之爬取虎扑湖人专区新闻中科比相关新闻
- linux 使用ssr客户端_React从CSR到SSR:第一篇
- 数据结构c语言版 严蔚敏 课本源码
- 几款win10下的小工具:截图、录屏、置顶窗口
- 小凯的疑惑(Noip 提高组 2017 d1 1)+[USACO4.1]麦香牛块Beef McNuggets
- 科研写作之word技巧——首行缩进、图 表 公式 自动编号和引用、子标题细节
- vue.js中的created()与activated()的用法
- Office 365 API平台概览
- 文章4:多功能智能跟随行李箱控制系统设计 | 本科毕业设计 - 【资料搜索技巧+参考资源整合】
- 缺省波特率57600
- 10:满足条件的数累加
- VMware共享文件夹设置
- c陷进与缺陷笔记(1)
- ssm+JSP计算机毕业设计游戏网站设计n8q96【源码、程序、数据库、部署】
- [网络工程师]-网络规划与设计-物理网络设计
热门文章
- web重启tomcat_使用docker构建jdk镜像和web服务镜像
- MTK:GPIO模式与设置
- android 点击通知栏打开activity,Android实现点击通知栏后,先启动应用再打开目标Activity...
- java httpclient 进度条_SpringBoot如何实现一个实时更新的进度条的示例代码
- uvm 形式验证_6.小白学uvm验证 - 寄存器模型
- python导入dat数据_Kaggle影评数据集,Python数据分析小例子14
- 字符串用法大全(源码解析,建议收藏!)
- Angular 启动项目时 port 4200 is already in use 解决方法
- highlightjs 详解
- 大数据-实验2 熟悉常用的HDFS操作