动态数据生成静态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

智能推荐

将本地网站发布到服务器上_网页发布-程序员宅基地

文章浏览阅读1.5k次,点赞2次,收藏4次。将静态的网页部署到服务器上,首先需要下载一个xftp,帮助我们上传文件;以及它的服务端xshell帮助我们重启服务器1,首先与服务器建立连接2,要想把我们的静态网页发布到服务器上,前提是我们的服务器安装了nignx,完成这些以后把我们本地的网页文件夹上传到/usr/local/nginx/html的路径下3,文件上传成功后,还需要我们修改nignx的配置文件,打开/usr/local/ng..._网页发布

java.net.ConnectException: no available server-程序员宅基地

文章浏览阅读2.1w次,点赞8次,收藏10次。我出现这个错误是因为没有加载我的配置文件在这里插入图片描述可以看到我上图打印的是连接我本地的nacos,并且连接超时但实际我的配置文件并不是配置的本地,看下图这就是典型的没有加载解决办法:完成这些就能加载到配置文件了..._no available server

请求大佬帮忙看看VScode Tensorflow model.fit 报错_model.fit报错use_multiprocessing=use_multiprocessing-程序员宅基地

文章浏览阅读1k次。import numpy as npimport tensorflow_core as tffrom tensorflow.keras.layers import Dense, SimpleRNNimport matplotlib.pyplot as pltimport osinput_word = "abcde"w_to_id = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4} # 单词映射到数值id的词典id_to_onehot = {0: [1.,._model.fit报错use_multiprocessing=use_multiprocessing

chapter04-程序员宅基地

文章浏览阅读152次。1、创建/guanli 目录,在/guanli下创建zonghe 和 jishu 两个目录(一条命令)[root@localhost ~]mkdir -p /guanli/{zonghe,jishu}2、添加组帐号zonghe、caiwu、jishu,GID号分别设置为2001、2002、2003[root@localhost ~]# groupadd -g 2001 zon..._在chapter04的包cn.itcast.chapter04.prsponse

字符串_空串是什么都没有字符串吗-程序员宅基地

文章浏览阅读362次。串的定义串是字符串的简称。在数据结构中,串是一种在数据元素的组成上具有一定约束条件的线性表,即要求组成线性表的所有数据元素都是字符,所以说串是一个有穷的字符序列。串是由零个或多个字符组成的有限序列,记作s=”s0s1…sn-1”(n≥0),其中s是串名,字符个数n称作串的长度,双撇号括起来的字符序列”s0s1…sn-1”是串的值字符串(String)是由数字、字母、任何其它的符号组成的一串字符。零个字符的串(即:"")称为空串,空串不包含任何字符。值得注意的是:(1)长度为1的空格串" “不等同于_空串是什么都没有字符串吗

mathtype软件产品密钥最新7.4_mathtyoe产品密钥-程序员宅基地

文章浏览阅读1.5k次,点赞15次,收藏18次。总之,MathType 7.4是一款功能全面、易于使用的数学公式编辑器,无论是教育、科研还是出版领域,它都能为用户提供强大的支持和便利。当然,作为软件技术支持专家,我很乐意为用户提供关于MathType 7.4的详细信息和支持。_mathtyoe产品密钥

随便推点

【SQL注入漏洞-04】布尔盲注靶场实战_oracle布尔盲注-程序员宅基地

文章浏览阅读6.5k次,点赞4次,收藏3次。当我们改变前端页面传输给后台sql参数时,页面没有显示相应内容也没有显示报错信息时,不能使用联合查询注入和报错注入,这时我们可以考虑是否为基于布尔的盲注。利用页面返回的布尔类型状态,正常或者不正常;我们输入的语句让页面呈现出两种状态,相当于true和false,根据这两种状态可以判断我们输入的语句是否查询成功。布尔盲注就是根据这两种状态,来反推我们输入的条件是真还是假。以sqli-labs-masterless-8关为例_oracle布尔盲注

ROS学习(11)使用ROS创建地图_ros建图-程序员宅基地

文章浏览阅读1w次,点赞10次,收藏72次。创建地图是一件比较复杂的工作,ROS利用map_server地图服务器,借助激光雷达和机器人的里程信息来完成这项工作。本篇我们还是利用柳树车库作为默认的地图环境。主要介绍了地图的创建、保存、加载,下一篇尝试配置导航功能包集,并在gazebo仿真环境下完成自定义机器人的自主导航。httpshttpshttpshttps。..._ros建图

自定义Magento页标题与Meta描述_magento seo suite 自定义 meta description-程序员宅基地

文章浏览阅读3.2k次。在Magento中,CMS页、产品页、分类页均可以设置Meta keywords与Meta Description。但在其它页面上如何设置呢?例如今天SEO团队发来文档,要求修改Checkout页、MyAccount页、Login页、Contact页等等的页标题与Meta描述部分。于是第一反应就是用XML来配置.另:在System-Configration-Design-Html-_magento seo suite 自定义 meta description

【PTA-python】第4章-15 换硬币 (20 分)_pta换硬币python-程序员宅基地

文章浏览阅读1.3k次。第4章-15 换硬币分析题目解法分析为了实现各个硬币数目>=1,range()函数设定倒序范围,先求五分硬币数目,再求二分硬币数目,最后求一分硬币数目,注意在往下递推求解的过程中,各个硬币数目的条件是>=1,这影响到range(five,0,-1)和if one>=1:题目将一笔零钱换成5分、2分和1分的硬币,要求每种硬币至少有一枚,有几种不同的换法?输入格式:输入在一行中给出待换的零钱数额x∈(8,100)。输出格式:要求按5分、2分和1分硬币的数量依次从大到小的顺序_pta换硬币python

firewall限制或开放IP及端口命令_firewall-cmd --zone=public --list-ports-程序员宅基地

文章浏览阅读1w次,点赞6次,收藏23次。一、查看防火墙状态1、首先查看防火墙是否开启,如未开启,需要先开启防火墙并作开机自启systemctl status firewalld开启防火墙并设置开机自启systemctl start firewalldsystemctl enable firewalld一般需要重启一下机器,不然后面做的设置可能不会生效二、开放或限制端口1、开放端口(1)如我们需要开启XShell连接时需要使用的22端口firewall-cmd --zone=public --add-port=22/tcp _firewall-cmd --zone=public --list-ports

北京交通大学-图像处理与机器学习_北京交通大学图像处理与机器视觉实验代码-程序员宅基地

文章浏览阅读6.7k次,点赞10次,收藏73次。日期项目2022/03/04更新实验环境配置视频P6,第八章后所有内容已同步更新!名称项目课程主页图像处理与机器学习B站主页啥都会一点的研究生课程B站视频【北交】图像处理与机器学习人工智能技术探讨群178174903人工智能技术探讨群2571218304人工智能技术探讨群3584723646课程目录及资料(UP已打包整理支持直链下载)章节内容资料第一章 绪论1-1 基本._北京交通大学图像处理与机器视觉实验代码

推荐文章

热门文章

相关标签