Asp. Net generates html page code instances

Source: Internet
Author: User

Ideas
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.

Advantages
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.

Disadvantages
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 <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 to the stringbuilder object ----

String [] format = new string [4]; // defines an array with the same number of htmlyem tags
StringBuilder htmltext = new StringBuilder ();
Try
{
Using (StreamReader sr = new StreamReader ("path for storing template pages and Page name "))
{
String line;
While (line = sr. ReadLine ())! = Null)
{
Htmltext. Append (line );
}
Sr. Close ();
}
}
Catch
{
Response. Write ("<Script> alert (File Read 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 ------------------――

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 cocould not be wirte :");

}
Summary: 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.