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

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了asp.net实现生成静态页并添加链接的方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文以实例讲解了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实现生成静态页并添加链接的方法全部内容,希望文章能够帮你解决asp.net实现生成静态页并添加链接的方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。