asp.net实现生成静态页并添加链接的方法

所属分类: 网络编程 / ASP.NET 阅读数: 753
收藏 0 赞 0 分享

本文以实例讲解了asp.net实现生成静态页并添加链接的方法,非常实用的功能,通过本实例可以加深读者对于asp.net下文件操作的认识。

1.创建一个静态网页模板

<!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>
<title>模板网页</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<table $htmlformat[0] height="100%" border="0" width="100%" cellpadding="10" cellspacing="0" gcolor="#eeeeee" style="border:1px solid #000000">
<tr>
<td width="100%" valign="middle" align="left">
<h1>$htmlformat[1]</h1>
<div style="color: $htmlformat[2];font-size: $htmlformat[3]">$htmlformat[4]</div>
</td>
</tr>
</table>
</body>
</html>

2.在asp.net网页后台代码中替换模板html页中的标记符

protected void Button1_Click(object sender, EventArgs e)
{
string[] format = new string[5];//定义和htmlyem标记数目一致的数组
StringBuilder htmltext = new StringBuilder();
string templatePath = Server.MapPath("~/html/Template.html");
StreamReader sr = new StreamReader(templatePath);
String line;
while ((line = sr.ReadLine()) != null)
{
htmltext.Append(line);
}
sr.Close();
//---------------------给标记数组赋值------------
format[0] = "background=\"bg.jpg\"";//背景图片
format[1] = TB_Title.Text;
format[2] = "#990099"; //字体颜色
format[3] = "60px";//文字大小
format[4] = TB_Content.Text;
//----------替换htm里的标记为你想加的内容
for (int i = 0; i < 5; i++)
{
htmltext.Replace("$htmlformat[" + i + "]", format[i]);
}
//----------生成htm文件------------------――
string newfile = Server.MapPath("~/html/"+TB_Title.Text+".html");
StreamWriter sw = new StreamWriter(newfile, false, System.Text.Encoding.GetEncoding("GB2312"));
sw.WriteLine(htmltext);
sw.Flush();
sw.Close();
string newhref="html/" rel="external nofollow" +TB_Title.Text+".html";
Response.Write("<a href=\"" + newhref + "\">" + TB_Title.Text + "</a>");
}
更多精彩内容其他人还在看

asp.net JavaScript插件  JavaScript Function Outliner

一个JavaScript Function Outliner插件 第四版本 支持内嵌javascript,且可以对javascript进行压缩
收藏 0 赞 0 分享

asp.net汉字转拼音和获取汉字首字母的代码

在网上找到的好东西。以后asp.net下汉字转成拼音就方便多了
收藏 0 赞 0 分享

asp.net 多字段模糊查询代码

经常用到多字段的模糊查询,上面的函数可以实现,例如strKeyWords值为“脚本之家”时
收藏 0 赞 0 分享

OpenCms 带分页的新闻列表

有一些网友在新闻列表分页上还遇到一些问题,正好这个blog上也忘记了此部分内容,现在补充上,功能是实现了,可以自己再做些优化,OpenCms7.0.5下测试通过,内容如下(编辑器的插入代码功能有问题,就直接把代码粘上了
收藏 0 赞 0 分享

URLRewriter最简单入门介绍 URLRewriter相关资源

配置好后,查看日志看到的状态都是200,IIS直接认为这个文件是存在的了, 而不是301,或302,这在某些情况下可能会不适用,比如:搜索引擎优化时目录或文件调整。
收藏 0 赞 0 分享

asp.net Repeater取得CheckBox选中的某行某个值

Repeater取得CheckBox选中的某行某个值的实现代码
收藏 0 赞 0 分享

asp.net清空Cookie的两种方法

清空cookie
收藏 0 赞 0 分享

asp.net一些很酷很实用的.Net技巧第1/2页

方便使用asp.net编程的朋友,都是一些非常有用的东西
收藏 0 赞 0 分享

asp.net下URL网址重写成.html格式、RSS、OPML的知识总结

asp.net下URL网址重写成.html格式、RSS、OPML的知识总结
收藏 0 赞 0 分享

使用UserControl做网站导航条的思路 分析

使用UserControl做网站导航条的思路 分析
收藏 0 赞 0 分享
查看更多