asp.net实现生成静态页并添加链接的方法
本文以实例讲解了asp.net实现生成静态页并添加链接的方法,非常实用的功能,通过本实例可以加深读者对于asp.net下文件操作的认识。
1.创建一个静态网页模板
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <title>模板网页</title> <metahttp-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:1pxsolid#000000"> <tr> <tdwidth="100%"valign="middle"align="left"> <h1>$htmlformat[1]</h1> <divstyle="color:$htmlformat[2];font-size:$htmlformat[3]">$htmlformat[4]</div> </td> </tr> </table> </body> </html>
2.在asp.net网页后台代码中替换模板html页中的标记符
protectedvoidButton1_Click(objectsender,EventArgse)
{
string[]format=newstring[5];//定义和htmlyem标记数目一致的数组
StringBuilderhtmltext=newStringBuilder();
stringtemplatePath=Server.MapPath("~/html/Template.html");
StreamReadersr=newStreamReader(templatePath);
Stringline;
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(inti=0;i<5;i++)
{
htmltext.Replace("$htmlformat["+i+"]",format[i]);
}
//----------生成htm文件------------------――
stringnewfile=Server.MapPath("~/html/"+TB_Title.Text+".html");
StreamWritersw=newStreamWriter(newfile,false,System.Text.Encoding.GetEncoding("GB2312"));
sw.WriteLine(htmltext);
sw.Flush();
sw.Close();
stringnewhref="html/"rel="externalnofollow"+TB_Title.Text+".html";
Response.Write("<ahref=\""+newhref+"\">"+TB_Title.Text+"</a>");
}热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下