Dynamically generate HTML pages with ASP.

Source: Internet
Author: User
Ideas
1. Use tools such as DW-MX to generate HTML-formatted templates, add special tags where they need to be added (such as $htmlformat$), dynamically generate files using code to read this template, and then get the content of the foreground input, added to the tag location of this template, Writes a new file name to the disk after it is written, and then writes the relevant data to the database.

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

Advantages

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

2. Static HTML file using the Index server of Ms Windows2000, you can build a full-text search engine, and use ASP. NET to get search results in a DataTable. The index service of Win2000 cannot find the contents of the XML file. This search feature is very powerful if you include a database search and index index double lookup.

3. Save server load, request a static HTML file to save a lot of resources than one ASPX file server resource.

Disadvantages

Second thought: If you use hard-coded methods, the workload is very large and requires a lot of HTML code. Difficult to debug. And the HTML style generated with hard-coded cannot be modified, and if the Web site is changed, it must be re-encoded, resulting in a tremendous amount of work later.

So here's the first way of thinking.

Code for the Display column

1. Define (template.htm) HTML Template page


2.asp.net code:

---------------------read the HTML template page into the StringBuilder object----string[] format=new string[4];//defines and Htmlyem the same number of 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 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 markup in the HTM with the content 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 being wirte:");}

Summary

This method makes it easy to generate HTML files. The program uses a loop substitution, so the template that needs to replace a large number of elements is very fast.

"Recommended"

1. an ASP. code example for generating an HTML form dynamically

2. How to share the HTML page dynamically generated by ASP

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.