动态数据生成静态html页_监听数据变更自动生成静态html-程序员宅基地

技术标签: button  server  textbox  string  fckeditor  html  # c#  

主要的原理就是替换模板里的特殊字符。

1、静态模板页面 template.html,主要是定义了一些特殊字符,用来被替换。

HTML code
   
   
    
<! 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,就是一个表单,用来填写新闻的标题和内容。

HTML code
   
   
    
<% @ 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 " %> <! 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

C# code
   
   
    
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 = " <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( " ../../ " ) + " NewsFiles " + " // " + strDate + " .shtml " ; strhtml = strhtml.Replace( " Pager " , "" ); } 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 " , " <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。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/fly_to_the_winds/article/details/3928209

智能推荐

一、Jquery入门(超详)-程序员宅基地

文章浏览阅读4.3k次,点赞21次,收藏48次。本文将带领大家了解 jQuery 的定义,它有什么作用,我们为什么要学它,以及如何使用它,它的语法是什么,最后对比了 jQuery 对象和 DOM 对象的区别。_jquery

【SassError: expected selector报错 用::v-deep 替换 /deep/的真实写法】_unexpected unknown pseudo-element selector "::v-de-程序员宅基地

文章浏览阅读2.7k次,点赞2次,收藏2次。关于SassError: expected selector报错 ::v-deep 替换 /deep/的正确替换方式_unexpected unknown pseudo-element selector "::v-deep

Linux学习笔记 第0章:计算机概论_字长 位宽-程序员宅基地

文章浏览阅读301次。0.1.0计算机本质计算机:接受用户输入的命令与数据,经由中央处理器的算术与逻辑单元运算处理后产生储存成有用的信息算术逻辑单元(Arithmetic&logical Unit:是中央处理器(CPU)的执行单元,是所有中央处理器的核心组成部分,由与门 和或门构成的算术逻辑单元,主要功能是进行二位元的算术运算,如加减乘(不包括整数除法)。基本上,在所有现代CPU体系结构中,二进制都以补码的..._字长 位宽

Curve25519 Field域2^255-19内的快速运算_curve25519标量乘-程序员宅基地

文章浏览阅读1.2k次,点赞3次,收藏3次。1. 引言对于Curve25519,其Field域内的module Fp = 2255-19。若采用常规的Montgomery reduce算法,其运算性能并不是最优的。如要求某整数 u mod (2^255-19),可将m整数用多项式做如下表示:u=∑iuixi,其中,ui=n∗2⌈25.5i⌉,n∈Nu=\sum_{i}^{}u_ix^i,其中,u_i=n*2^{\left \lce..._curve25519标量乘

超全golang面试题合集和答案+golang学习指南+golang知识图谱_goland面试题-程序员宅基地

文章浏览阅读6.8k次,点赞3次,收藏68次。https://blog.51cto.com/u_15102959/2637222_goland面试题

Python实现自动挂机脚本(基础篇)_我是农场主自动挂机脚本-程序员宅基地

文章浏览阅读8.4w次,点赞67次,收藏608次。不知不觉肝阴阳师也快一年了,对这游戏真是又爱又恨,最近刚刚发布了PC版,突然很想尝试着写个脚本挂机,话不多说进入正题。简单的鼠标操作游戏挂机脚本,无非就是自动移动鼠标,自动点击,进行重复操作,所以,第一步就是如何控制鼠标_我是农场主自动挂机脚本

随便推点

如何用NAS打造私有协同办公系统?-程序员宅基地

文章浏览阅读6.2k次。对于人数不多的小型初创企业、工作室、SOHO人群来说,能够拥有自有的协同办公系统无疑是提高工作效率的好方法,同时将文件放在自己的服务器中,显然会更加安心,不用担心重要内容的泄露问题。因此,大家有没有这样想过,自己动手搭一套私有的、云端化的协同办公系统,搞定文件异地同步的同时,实现云端化的办公软件,并提升数据安全性。理想虽好,不过要亲手搞定这样的协同办公系统一定很困难吧?如果你真这样

假设你们的社团要精选社长,有两名候选人分别是A和B,社团每名同学必须并且只能投一票,最终的票多的人为社长。-程序员宅基地

文章浏览阅读33次。输出描述:一行,一个字符,A或B或E,输出A表示A得票数多,输出B表示B得票数多,输出E表示二人得票数相等。输入描述:一行,字符序列,包含A或B,输入以字符0结束。

BeanFactory和ApplicationContext有什么区别?_beanfactory和applicationcontext是干什么的-程序员宅基地

文章浏览阅读2.2k次,点赞2次,收藏2次。BeanFactory和ApplicationContext有什么区别? BeanFactory和ApplicationContext是Spring的两大核心接口,都可以当做Spring的容器。其中ApplicationContext是BeanFactory的子接口。(1)BeanFactory:是Spring里面最底层的接口,包含了各种Bean的定义,读取bean配置文档,管理..._beanfactory和applicationcontext是干什么的

java 项目管理 maven2.0学习笔记 _apt fml fr-程序员宅基地

文章浏览阅读4.5k次。转贴:http://blog.csdn.net/shiqiang1234/archive/2006/10/12/1331725.aspxMaven简介Maven最初的目的是在Jakarta Turbine项目中使构建处理简单化。几个项目之间使用到的Ant build文件差异很小,各个JAR都存入CVS。因此希望有一个标准的方法构建各个工程,清晰的定义一个工程的组成,一个容易的方法去发布项目_apt fml fr

【linux之进程间通信】——管道_管道read-程序员宅基地

文章浏览阅读2.6k次,点赞9次,收藏11次。进程间是怎么通信的之前我们说过,进程与进程之间是相互独立的,它们的数据也是绝对独立的,那么如果一个进程要向另一个进程发送数据,发送消息等通信,那么这时怎么做到的呢?管道所谓的管道,是内存中的一个缓存文件,进程A在管道写入数据,实际是写入到内核中的缓存文件中,进程B从管道中读取数据,实际是从内核的缓存文件中读取数据。管道的传输数据是单向的,只能由一个进程写入,一个进程读出。匿名管道概念匿名管道是没有名字的,它是特殊文件,它只存在内存中,不存在我们文件系统中,即用完就销._管道read

DermoSegDiff: A Boundary-aware Segmentation Diffusion Model for Skin Lesion Delineation-程序员宅基地

文章浏览阅读445次,点赞5次,收藏10次。DermoSegDiff:用于皮肤病变描绘的边界感知分割扩散模型摘要:皮肤病变分割对皮肤病的早期发现和准确诊断起着至关重要的作用。消噪扩散概率模型(ddpm)最近因其出色的图像生成能力而受到关注。在这些进展的基础上,我们提出了DermoSegDiff,这是一个在学习过程中包含边界信息的皮肤病变分割的新框架。我们的方法引入了一种新的损失函数,在训练过程中对边界进行优先排序,逐渐降低其他区域的重要性。我们还介绍了一种新的基于u - net的去噪网络,该网络可以熟练地将网络内的噪声和语义信息集成在一起。在多个_dermosegdiff