C # dynamically generate html pages

Source: Internet
Author: User

Transfer (http://www.soaspx.com/dotnet/csharp/csharp_20090902_265.html)

This function is applicable to websites with poor background database functions, that is, most of the text is not stored in the database records, but in html or xml files, and only the index is placed in the database, such as the title, category, and keyword of an article. This method is suitable for websites without database support such as MS SQL Server in the background.

Applicable to news publishing systems, such as sina and 163, which dynamically generate html pages.

Applicable to programs that require dynamic page customization. Such as forums and chat rooms. You can load custom html pages to enhance the appearance.


1. use a tool such as Dw-Mx to generate an html template, and add special tags (such as $ htmlformat $) where the format needs to be added. When a file is dynamically generated, use the code to read the template, obtain the content entered at the front end, add it to the tag location of this template, generate a new file name, and write it to the disk. Then, write the data to the database.

2. Html files are hardcoded Using Background code. You can use the HtmlTextWriter class to write html files.


1. you can create complex pages and add document to the js file by using methods that contain js files. the write () method can be used to add content such as the page header and advertisement to all pages.

2. The Index Server of MS Windows2000 can be used to create a full-text search engine for static html files, and asp.net can be used to obtain search results in DataTable mode. The Index Service of Win2000 cannot find the content of xml files. If both database search and Index search are included, this search function is very powerful.

3. Saving server load and saving a lot of resources by requesting a static html file than An aspx file server.


Train of Thought 2: If hard encoding is used, the workload is very heavy and a lot of html code is required. Debugging is difficult. In addition, html styles generated by hard encoding cannot be modified. If the style of a website is changed, it must be re-encoded, which brings a huge workload in the future.

Therefore, the first approach is adopted here.

Column code

1.define (template.htm) html template page

<Title> </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Table $ htmlformat [0] height = "100%" border = "0" width = "100%" cellpadding = "10" cellspacing = "0" bgcolor = "# eeeeee" style = "border: 1px solid #000000 ">
<Td width = "100%" valign = "middle" align = "left">
<Span style = "color: $ htmlformat [1]; font-size: $ htmlformat [2]"> $ htmlformat [3] </span>

2.asp.net code:

Public void WriteFile ()
// --------------------- Read the html template page to the stringbuilder object ----
String [] format = new string [4]; // defines an array with the same number of htmlyem tags

StringBuilder htmltext = new StringBuilder ();
Using (StreamReader sr = new StreamReader (Server. MapPath (".") + "/Test/HTMLPage2.htm") // template page path
String line;
While (line = sr. ReadLine ())! = Null)
Htmltext. Append (line );
Sr. Close ();
Response. Write ("<Script> alert ('file reading 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 content marked in htm with the content you want to add
For (int I = 0; I <4; I ++)
Htmltext. Replace ("$ htmlformat [" + I + "]", format [I]);

// ---------- Generate the htm file ------------------――
Using (StreamWriter sw = new StreamWriter (Server. mapPath (". ") +"/Test/test.htm ", false, System. text. encoding. getEncoding ("GB2312") // Save the address
Sw. WriteLine (htmltext );
Sw. Flush ();
Sw. Close ();

Response. Write ("The file cocould not be wirte :");


This method can be used to easily generate html files. The program uses cyclic replacement, so it is very fast for templates that need to replace a large number of elements.




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.