Dynamically generate HTML pages using ASP.NET technology

  • 2020-05-05 11:06:51
  • OfStack

Thinking
1. The use of tools to generate such as Dw - Mx html format templates, in the place where you need to add the format to join special tags (such as $htmlformat $), dynamically generated file using the code read this template, and then obtain the content of the input at the front desk, added to this template tag location, generate new file to disk, write and then write the relevant data into the database.

2. Use background code to hard-code Html files. You can use the HtmlTextWriter class to write html files.

Advantages

1. You can create very complex pages. By using the method of js file, you can add document to js file.

2. The static html file can be used to build a full-text search engine using MS Windows2000's Index Server, and asp net can be used to get search results in DataTable. The Index service of Win2000 could not find the contents of the xml file. This search is very powerful if you include both database search and Index index double lookup.

3. Save server load, request a static html file than an aspx file server resources savings.

Faults

Idea 2: if you use hard coding, the workload is very large, need a lot of html code. Debugging is difficult. Moreover, the html style generated by hard coding cannot be modified. If the website changes the style, it will have to be recoded, bringing huge workload to the later stage.

So the first way of thinking is

Listing code

1. Definition (template.htm)html template page

< html >
< head >
< title > www.knowsky.com < /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 >  

asp.net code:

/ / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- read html template page into -

stringbuilder object

string [] format = new string [4]. // defines an array
with the same number of tags as htmlyem StringBuilder htmltext=new StringBuilder();
try
{
using (StreamReader sr = new StreamReader(" path and page name for template pages "))
{
String line;
while ((line = sr.ReadLine ()))! =
null) {
htmltext. Append (line);
}
sr. Close ();
}
}
catch
{
Response. Write (" < Script > alert(' read file error ')< /Script > ");
}

/ / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- to tag array assignment -- -- -- -- -- -- -- -- -- -- -- --

format [0] = "background = \" bg jpg \ ""; // background image
format [1] = "# 990099". // font color
150 px format [2] = ""; // font size
format[3]= " < marquee > Generated template html page < /marquee > "; // text description
//-- replace the tag in htm with the content you want to add
for(int i=0;i < 4;i++)
{
htmltext. Replace (" $htmlformat [" + i + "] ", format [i]);
}

/ / -- -- -- -- -- -- -- -- -- -- generated htm file -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- � �

try
{
using (StreamWriter sw = new StreamWriter (" repository paths and page names, "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 makes it easy to generate html files. The program USES cyclic substitution, so it is very fast for templates that need to replace a large number of elements.


 


Related articles: