首 页 ┆ 源码下载 ┆ IT学院 ┆ 字体下载 ┆ 模板下载 ┆ 源码发布 ┆ 广告合作 ┆ 网站地图
► 设为首页
► 加入收藏
► 联系我们
源码下载 >> ASP源码 | PHP源码 | ASP.net源码 | JSP源码 | CGI源码 | VC/C++源码 | VB源码 | Delphi源码 | Flash源码
文章学院 >> 网络编程 | 网页设计 | 图形图象 | 数据库 | 服务器 | 网络媒体 | 网络安全 | 操作系统 | 办公软件 | 软件开发 | 黑客知识
字体下载 >> 精制字体 | 非英字体 | 艺术字体 | 著名字体 | 哥特式 | 简单字体 | 手写体 | 节假日 | 图案字体 | 精度像素 | 中文字体
模板下载 >> 企业门户 | 数码网络 | 休闲娱乐 | 影视音乐 | 旅游名胜 | 文化艺术 | 电子商务 | 个性展示 | 登陆导航 | Flash模板
►►您当前的位置:源码园 → IT学院 → 网页设计 → Html/Css → 文章内容

根据页面模板动态生成html页面

作者:佚名  来源:转载  发布时间:2008-1-7 14:28:01

一直以为动态生成静态页面不好做,昨天在网上找了下,其实很简单,思路大概是这样的,
1:建立一个html页面模板,在这个页面中把你想要动态显示的地方用特殊的字符串表示(如
$htmlstrstr$);
2:在程序中用将这个html页面读到一个字符串变量如str;
3:用字符串的resplace方法将在第一步中特殊字符替换成你想要的内容;
4保存;
OK,so easy,今天就用C#写了一个这样的类,用来处理动态生成html页面的,自认为还写的完
整,刚接触.NET不久,望指教


注:此类中的代码不全是原创,部份代码参照网友的代码!

以下是转换类的代码


代码
1using System;
2using System.Text;
3using System.Web;
4using System.Configuration;
5using System.IO;
6namespace solucky
7{
8 /**////


9 /// AspxToHtml 的摘要说明。
10 /// 注:使用此类,你可以在web.config文件对模板类进行配置.如下
11 /**//*
12
13
14
15 */
16 /**////
17 public class AspxToHtml
18 {
19 /**////
20 /// 模板文件中要替代的参数个数
21 ///
22 private int _templateParamCount=0;
23 /**////
24 /// 模板文件所在的路径
25 ///
26 private string _templateFilePath
=ConfigurationSettings.AppSettings["templateFilePath"];
27 /**////
28 /// 转换后的html文件所存放的路径
29 ///
30 private string _htmlFilePath
=ConfigurationSettings.AppSettings["htmlFilePath"];
31
32 /**////
33 /// 模板页页面编码
34 ///
35 private Encoding _templateHtmlCode
=Encoding.GetEncoding("gb2312");
36
37 /**////
38 /// 转换后的文件编码
39 ///
40 private Encoding _code = Encoding.GetEncoding("gb2312");
41
42 /**////
43 /// 转换后的html文件名
44 ///
45 private string _convertedFilename="";
46 /**////
47 /// 模板文件中的参数
48 ///
49 private string[] _templateFileparameter ;
50
51 /**////
52 /// aspx文件中的要代替HTML文件中的参数实际值
53 ///
54 private string[] _aspxFileparameter;
55
56 private string _errlogPath = ConfigurationSettings.AppSettings["ErrLogPath"];
57
58 属性#region 属性
59
60 /**////
61 /// 模板文件中要替代的参数个数
62 ///
63 public int TemplateParamCount
64 {
65 get
66 {
67 return this._templateParamCount;
68 }
69 set//分配参数个数时,同时为模板文件中的参数和aspx文件中的要代替
HTML文件中的参数实际值这两个分配实际数组
70 {
71 if (value < 0)
72 throw new ArgumentException();
73
74 if(value>0)
75 {
76 this._templateParamCount=value;
77 //模板文件中的参数
78 _templateFileparameter = new string[value];
79 //aspx文件中的要代替HTML文件中的参数实际值
80 _aspxFileparameter = new string[value];
81 }
82 else
83 this._templateParamCount=0;
84 }
85 }
86
87 /**////
88 /// 模板文件所在的路径
89 ///
90 ///
91 public string TemplateFilePath
92 {
93 get{ return this._templateFilePath;}
94 set{ this._templateFilePath=value;}
95 }
96 /**////
97 /// 转换后的html文件所存放的路径
98 ///
99 public string HtmlFilePath
100 {
101 get{ return this._htmlFilePath;}
102 set{ this._htmlFilePath=value;}
103 }
104
105 /**////
106 /// html模板文件编码
107 ///
108 public Encoding TemplateHtmlCode
109 {
110 get{ return this._templateHtmlCode;}
111 set{ this._templateHtmlCode=Encoding.GetEncoding(value.ToString());}
112 }
113 /**////
114 /// 编码
115 ///
116 public Encoding Code
117 {
118 get{ return this._code;}
119 set{ this._code=Encoding.GetEncoding(value.ToString());}
120 }
121 /**////
122 /// 错误文件所在路径
123 ///
124 public string ErrLogPath
125 {
126 get{
127 if(!(this._errlogPath==null))
128 return this._errlogPath;
129 else
130 return "aspxTohtml_log.txt";
131 }
132 set{this._errlogPath=value;}
133 }
134
135
136 #endregion
137
138 操作#region 操作
139
140 /**////
141 /// 获取转换后的html文件所在相对文件路径
142 /// 如:如果HtmlFilePath="/news/"
143 /// 转换后的html文件名为200505050505.html
144 /// 则返回的值为/news/200505050505.html
145 ///
146 /// 如果在未调用StartConvert方法之前调用此属性则返回
null
147 public string HtmlFileVirtualPath
148 {
149 get
150 {
151 if(!(this._convertedFilename==""))
152 return this.HtmlFilePath+this._convertedFilename;
153 else
154 return null;
155 }
156 }
157
158 /**////
159 /// 为HTML页面参数数组付值
160 ///
161 ///
162 public void setTemplateFileparameter(string[] param)
163 {
164 try
165 {
166 if(param.Length==this.TemplateParamCount)
167 this._templateFileparameter=param;
168 //else//与原定义的个数不等
169 //
170 }
171 catch(System.Exception ex)
172 {
173 WriteErrFile(ex);
174 }
175 }
176 /**////
177 /// 为aspx文件中将要替换html文件中的参数数组付值
178 ///
179 ///
180 public void setAspxFileparameter(string[] param)
181 {
182 try
183 {
184 if(param.Length==this.TemplateParamCount)
185 this._aspxFileparameter=param;
186 //else//与原定义的个数不等
187 //
188 }
189 catch(System.Exception ex)
190 {
191 WriteErrFile(ex);
192 }
193 }
194 /**////
195 /// 开始进行aspxTohtml转换
196 ///
197 /// 返回值为成功创建后的文件名称
198 /// 在调用此方法之前必需确

[1] [2] [3]  下一页

[] [返回上一页] [打 印]
  • 上一篇文章:警告RealPlayer 11漏洞
  • 下一篇文章:Windows Vista系统:揪出控制面板中的另一个管理帐户

  • 相关文章:
  • 根据页面模板动态生成html页面
  • Asp.net动态生成html页面
  • bean里面如何打印到html页面
  • ASP.NET生成静态HTML页面并分别按年月目录存放
  • JSP页面自动生成html页面或任何格式页面
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 源码发布
Copyright © 2003-2009 Ymyasp.Com. All Rights Reserved .
备案序号:粤ICP备07029071号