asp. net implements a method to generate static pages and add links

  • 2021-01-03 20:52:37
  • OfStack

This article explains asp.net's method of generating static pages and adding links with examples, which is very practical. This example can deepen readers' understanding of file operation under ES3en.net.

1. Create a static web page template


<!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> The template page </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. Replace the tag in the asp. net page in the asp. net page background code


protected void Button1_Click(object sender, EventArgs e)
{
string[] format = new string[5];// Define and htmlyem Tag number 1 To an array of 
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();
//--------------------- Assign a value to an array of tags ------------
format[0] = "background=\"bg.jpg\"";// The background image 
format[1] = TB_Title.Text;
format[2] = "#990099"; // The font color 
format[3] = "60px";// Text size 
format[4] = TB_Content.Text;
//---------- replace htm The tag in is what you want to add 
for (int i = 0; i < 5; i++)
{
htmltext.Replace("$htmlformat[" + i + "]", format[i]);
}
//---------- generate htm file ------------------ � � 
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>");
}

Related articles: