asp.net dynamically generate HTML pages (template technology)

Source: Internet
Author: User
Brief introduction

This feature is suitable for Web sites with weak background database functions, that is, most of the text is not stored in the records of the database, but in the HTML file or XML file, just put the index into the database, such as the title of the article, category, query keywords and so on. This is appropriate for Web sites in the background that do not have database support such as MS SQL Server.

Apply to the news release system, such as Sina, 163, etc. are dynamically generated HTML page.

Applies to programs that need to dynamically customize the page. such as forums, chat rooms and so on. You can load custom HTML pages to enhance your appearance.

   train of Thought

1. Use tools such as DW-MX to generate HTML-formatted templates, add special tags (such as $htmlformat$) where needed, and dynamically generate files using code to read the template, and then get the foreground input to add to the tag location of the template, Writes the new file name to the disk and writes the relevant data to the database after it is written.

2. Using background code to hard-code HTML files, you can use the HtmlTextWriter class to write HTML files.

   Advantages

1. Can be a very complex page, using the method containing JS files, in the JS file to add document.write () method can be added to all pages, such as page header, advertising and other content.

2. Static HTML files Use MS Windows2000 's Index server to create Full-text search engines, using asp.net to get search results in a DataTable. The Win2000 Index Service was unable to find the contents of the XML file. If you include a database search and index index double lookup, then this search function will be very powerful.

3. Save the load on the server and request a static HTML file to save a lot more than an ASPX file server resource.

   Disadvantage

Idea two: If the hard coded way, the workload is very large, need a lot of HTML code. Debugging difficult. And the HTML style generated with hard coding cannot be modified, and if the site is replaced, it must be coded to bring a huge workload to the later stage.

So here's the first way to take it.

Column Code

1. Define (template.htm) HTML Template page



<title></title>

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">


<body >

<table $htmlformat [0] height= "100%" border= "0" width= "100%" cellpadding= "ten" cellspacing= "0"

Bgcolor= "#eeeeee" style= "border:1px solid #000000" >

<tr>

&LT;TD width= "100%" valign= "Middle" align= "left" >

<span style= "COLOR: $htmlformat [1];font-size: $htmlformat [2]" > $htmlformat [3]</span>

</td>

</tr>

</table>

</body>


2.asp.net Code:

---------------------read the HTML template page to the StringBuilder object----

String[] Format=new string[4];//defines an array with a consistent number of Htmlyem tags

StringBuilder htmltext=new StringBuilder ();

Try

{

using (StreamReader sr = new StreamReader ("path and page name for Template page"))

{

String Line;

while (line = Sr. ReadLine ())!= null)

{

HTMLText. Append (line);

}

Sr. Close ();

}

}

Catch

{

Response.Write ("<script>alert (' Read file error ') </Script>");

}

---------------------assign a value to an array of tokens------------

format[0]= "background=/" bg.jpg/"";//Background picture

Format[1]= "#990099";//Font Color

Format[2]= "150px";//font size

format[3]= "<marquee> generated template HTML page </marquee>";//Text description

----------Replace the tags in htm for what you want to add

for (int i=0;i<4;i++)

{

HTMLText. Replace ("$htmlformat [" +i+ "]", format[i]);

}

----------generate an HTM file------------------??

Try

{

Using (StreamWriter Sw=new StreamWriter ("Store 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 makes it easy to generate HTML files. The program uses a circular replacement, so the template that needs to replace a large number of elements is fast.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.