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

智能推荐

Linux:进程创建-等待-退出_linux 进程创建 退出-程序员宅基地

文章浏览阅读626次。1、进程的创建1.1:fork函数pid_t fork(void)返回值:父进程返回子进程id,子进程返回0,出错返回-1当进程调用fork时,内核中的fork都做下面的事情 &amp;amp;gt;分配新的内存块和内核数据给子进程将父进程的部分数据结构拷贝到子进程中添加子进程到系统进程列表中fork返回,开始调度器调度eg:#include&amp;amp;lt;stdio.h&amp;amp;g..._linux 进程创建 退出

开源网络准入软件packetfence 部署心得-1-程序员宅基地

文章浏览阅读1.1w次。 packetfence 是一款内网准入软件,刚开始研究的时候也是一脸懵逼,资料少的可怜,前后玩了几个月,中途很多次都想放弃了,填完了很多坑,最后也算是成功了好了,今天就讲一下packetfence所能达到的功能吧简单来说,就是这么几步:1、内网环境说明:假设公司有3个网段,办公网段vlan10,访客网段vlan20,隔离网段vlan302、PC、笔记本等网络设备第一次接入网络,网段..._packetfence

2024年数学建模竞赛汇总——时间轴_2024电工杯数学建模-程序员宅基地

文章浏览阅读5.3k次,点赞28次,收藏41次。美赛已过,好多小伙伴表示已经错过,不清楚什么时候报名,什么时候准备,其实每年数学建模比赛有很多个,各大比赛的级别、报名时间、参赛对象等要求什么呢?小编从方面详细汇总了19个建模比赛,还在担心自己错过机会吗?码住这篇,一定要看到最后!_2024电工杯数学建模

Android 8.1 设置USB传输文件模式(MTP)_adb 设置usb为传输文件模式-程序员宅基地

文章浏览阅读1k次,点赞10次,收藏8次。项目需求,需要在电脑端adb发送通知手机端接收指令,将USB的仅充电模式更改成传输文件(MTP)模式,便捷用户在我的电脑里操作内存文件,下面是我们的常见的修改方式。_adb 设置usb为传输文件模式

Visual Studio 2003 下载地址 V7.1各种版本官方下载网址_visual studio 2003下载-程序员宅基地

文章浏览阅读6.4k次。Visual Studio .NET 2003 简体中文版CD1下载地址:ftp://cnc-ftp3.a5d.com/download/sc_vs.net_2003_library_cd1.isoCD2下载地址:ftp://cnc-ftp3.a5d.com/download/sc_vs.net_2003_library_cd2.isoCD3下载地址:ftp://cnc-ftp3.a5d.c..._visual studio 2003下载

随便推点

echarts设置tooltip的层级_echart tooltip层级问题-程序员宅基地

文章浏览阅读773次,点赞9次,收藏7次。echarts设置tooltip的层级。_echart tooltip层级问题

Ubuntu下、ros下 vscode 常用快捷键_ubuntu vscode 快捷键-程序员宅基地

文章浏览阅读712次,点赞3次,收藏2次。缩小界面:“ctrl”+“-” 放大界面:“ctrl”+“+” 注释你所框选的行: “ctrl”+“/” 再次“ctrl”+“/” ,解除注释跳转到函数实现后,想返回到上一级: “ctrl”+“alt”+“-” 显示/隐藏左侧目录栏 :“ctrl”+“b”查找: “ctrl”+“f”快速回到顶部 : “ctrl”+“home”切换全屏 : F11打开终端:“ctrl”+“~” ( “ ~ ”在tab上面)_ubuntu vscode 快捷键

LangChain 53 深入理解LangChain 表达式语言十六 Dynamically route动态路由 LangChain Expression Language (LCEL)_langchain表达式lcel(六)-程序员宅基地

文章浏览阅读956次,点赞17次,收藏18次。本笔记本涵盖了如何在LangChain表达语言中进行路由。路由允许您创建非确定性链,其中上一步的输出定义了下一步。路由有助于在与LLMs的交互中提供结构和一致性。我们将使用两个步骤的序列来说明这两种方法,其中第一步将输入问题分类为关于LangChain、ChatGPT或其他,然后路由到相应的提示链。_langchain表达式lcel(六)

iOS开发进阶 - 使用shell脚本自动打包上传到fir.im上_fir.im 怎么通过sh命令上传apk和ip-程序员宅基地

文章浏览阅读1.1w次,点赞2次,收藏7次。如果移动端访问不佳,可以访问我的个人博客 用fir.im测试已经好长时间了,感觉每次打包上传都很麻烦,想着是不是可以用脚本自动打包,在网上搜了一下确实有,下面总结一下如何使用脚本自动打包上传到fir.im,以及打包过程中遇到的问题和解决办法相关资料和下载首先是打包脚本的下载地址,这个是我找到的比较全的一个,里面有很多不同功能的shell脚本,亲测好用,传送门还有关于fir指令的一些介绍可以去这_fir.im 怎么通过sh命令上传apk和ip

CentOS 7中搭建NFS文件共享存储服务的完整步骤_两台服务器centos7 文件共享-程序员宅基地

文章浏览阅读289次。CentOS 7中搭建NFS文件共享存储服务的完整步骤_两台服务器centos7 文件共享

vue3新项目要怎么配置eslint和prettier,不让他们冲突_vue3.0常用eslint配置-程序员宅基地

文章浏览阅读548次。创建项目的时候选了eslint和prettier,但是想配置一下让他们按照自己的风格来格式化,以前配置过,但是忘了,研究了一下午,记录一下。6、终端执行这个命令,项目应该就可以跑起来了,需要修改格式化规则或者配置eslint规则往对应的文件里加就行了。2、配置.eslintrc.js,这个是创建项目时选择eslint+prettier脚手架创建的。1、准备工作,安装下这两个插件,禁用下Vetur插件,已经装过的可以忽略。参考了以下大佬的文章,里面有相关的其他的知识详解,可以参考学习。_vue3.0常用eslint配置