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

2025-05-29 0 78

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

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17
<!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页中的标记符

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32
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>");

}

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 asp.net实现生成静态页并添加链接的方法 https://www.kuaiidc.com/103490.html

相关文章

发表评论
暂无评论