This function is suitable for web sites with weak backend database functions, that is, most of the text is not stored in database records, but in html files or xml files. Only indexes are placed in the database, such as article titles, categories, Query keywords, etc. This is suitable for Web sites that do not have database support such as MS Sql Server in the background.
Suitable for news release systems, such as sina, 163, etc., which use dynamically generated HTML pages.
Suitable for programs that need to dynamically customize pages. Such as forums, chat rooms, etc. Customized html pages can be loaded to enhance the aesthetics.
Idea
1. Use tools such as Dw-Mx to generate html format templates, add special tags (such as $htmlformat$) where the format needs to be added, use code to read this template when dynamically generating files, and then obtain the content input by the front desk. , added to the mark position of this template, the new file name is generated and written to the disk, and then the relevant data is written to the database.
2. Use the background code to hardcode the Html file. You can use the HtmlTextWriter class to write the html file.
Advantages
1. You can create very complex pages. By using the method of including js files, adding the document.write() method in the js file can add content such as page headers, advertisements, etc. to all pages.
2. Static html files can use the Index Server of MS Windows2000 to build a full-text search engine, and use asp.net to obtain search results in the form of DataTable. The Index service of Win2000 cannot find the contents of the xml file. If it includes database search and Index index dual search, then this search function will be very powerful.
3. Save server load. Requesting a static html file saves many server resources than an aspx file.
Disadvantage
idea two: If you use hard coding, the workload is very heavy and requires a lot of html code. Debugging is difficult. Moreover, the HTML style generated using hard coding cannot be modified. If the website changes the style, it must be recoded, which will bring a huge workload in the later stage.
Therefore, the first idea is used here
to list the code
1. Define (template.htm) html template page
<html>
<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" bgcolor="#eeeeee" style="border:1px solid #000000">
<tr>
<td width="100%" valign="middle" align="left">
<span style="color: $htmlformat[1 ];font-size: $htmlformat[2]">$htmlformat[3]</span>
</td>
</tr>
</table>
</body>
</html>
2.asp.net code:
/ /---------------------Read the html template page into the stringbuilder object----
string[] format=new string[4];//Definition and htmlyem Arrays with the same number of tags
StringBuilder htmltext=new StringBuilder();
try
{
using (StreamReader sr = new StreamReader("The path and page name to store the template page"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
htmltext.Append(line);
}
sr.Close();
}
}
catch
{
Response.Write("<Script>alert('Error reading file')</Script>");
}
// ---------------------Assign a value to the tag array---------------------
format[0]="background="bg.jpg "";//Background image
format[1]= "#990099";//Font color
format[2]="150px";//Font size
format[3]= "<marquee>Generated template html page< /marquee>";//Text description
//----------Replace the mark in html with the content you want to add
for(int i=0;i<4;i++)
{
htmltext.Replace( "$htmlformat["+i+"]",format[i]);
}
//----------Generate html file----------------- -----
try
{
using(StreamWriter sw=new StreamWriter("storage path and page name",false,System.Text.Encoding.GetEncoding("GB2312")))
{
sw.WriteLine(htmltext);
sw.Flush() ;
sw.Close();
}
}
catch
{
Response.Write ("The file could not be wirte:");
}
Summary
This method can be used to easily generate html files. The program uses loop replacement, so it is very fast for templates that need to replace a large number of elements.