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

所属分类: 网络编程 / ASP.NET 阅读数: 731
收藏 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>");
}
更多精彩内容其他人还在看

.NET 数据库连接池

如果您通过使用另一个 Execute 方法(例如,ExecuteScalar、ExecuteNonQuery 和 ExecuteXMLReader)执行查询
收藏 0 赞 0 分享

asp.net sqlconnection con.close和con.dispose区别

con.close是用来关闭和数据库的连接,相对于open
收藏 0 赞 0 分享

ASP.NET 多次提交的解决办法

只要把这2个方法放到页面最下面(就是调用scriptmanager的RegisterStartupScript方法)
收藏 0 赞 0 分享

ASP.NET 多次提交的解决办法2

对“添加”、“提交”、“保存”、“更新”等按钮需要对数据库进行写操作的按钮,一定要在页面初始化时加载脚本,防止多次重复点击
收藏 0 赞 0 分享

firebird Embedded模式(.net 3.5)

实现的关键:copy fbembed.dll icudt30.dll icuuc30.dll到system32文件夹下
收藏 0 赞 0 分享

js 父页中的单选按钮取值

js 父页单选按钮取值函数
收藏 0 赞 0 分享

js控制.net验证控件是否可用。

js .net验证控件的代码
收藏 0 赞 0 分享

ASP.NET AJAX时用alert弹出对话框

ASP.NET AJAX alert弹出对话框 解决 asp.net onClientClick 与 验证控件冲突问题
收藏 0 赞 0 分享

Asp.NET 多层登陆实现代码

昨天尝试学着PETSHOP的分层思想,写了个.NET下的登陆例子,不过比PETSHOP要精简很多,采用access数据库,方便学习。希望对大家有帮助。
收藏 0 赞 0 分享

aspx 中文汉字显示为乱码

要保证文件本身为utf-8编码格式。 .cs文件也是一样。
收藏 0 赞 0 分享
查看更多