Collected data (8) -asp.net Dynamic HTML page generation

Source: Internet
Author: User

Asp.netDynamic generationHtmlPage

 Asp.netDynamic generationHtmlPage

Applicable:Microsoft ASP. NET

Summary: Asp.netDynamic generationHtmlPage,Applicable

Introduction

This function is applicable to scenarios where the background database does not have strong functions. Web Site, that is, most of the text is not stored in the database records, but stored in Html File or XML File, just put the index in the database, suchArticleTitle, category, and query keyword. This is suitable for the background without such Ms SQL Server This database supports Web Site.

Applicable to news publishing systems, suchSina,163Dynamic generation is adopted.HtmlPage.

Applicable to pages that require dynamic customizationProgram. Such as forums and chat rooms. Can load customHtmlPage to enhance the appearance.

Ideas

1. exploitation is as follows: DW-mx this tool generates HTML format template, add a special tag ( for example, $ htmlformat $), use Code reads the template, obtains the content entered at the front-end, adds it to the tag location of the template, generates a new file name, and writes it to the disk, write data to the database.

2.Hard Coding Using Background codeHtmlFile, you can useHtmltextwriterClass to writeHtmlFile.

Advantages

1.You can create complex pages and useJSFile method, inJSFile AdditionDocument. Write ()You can add content such as the page header and advertisement to all pages.

2. Static Html File Exploitation MS Windows2000 Of Index Server You can create a full-text search engine and use Asp.net You can use Datatable To obtain the search result. While Win2000 Of Index Service cannot be searched XML File Content. If database search and Index Double search of indexes makes this search function very powerful.

3.Reduces server load and requests a staticHtmlFile to fileAspxThe file server saves a lot of resources.

Disadvantages

Thought 2: Hard coding requires a lot of work.HtmlCode. Debugging is difficult. In addition, hard-codedHtmlStyles cannot be modified. If the style of a website is changed, the style must be re-encoded, resulting in a huge workload in the future.

Therefore, the first approach is adopted here.

Column code

1.Definition(Template.htm) HTMLTemplate page

<HTML>

<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.netCode:

//---------------------ReadHtmlTemplate pageStringbuilderObject----

String [] format = new string [4]; //Definition andHtmlyemArray with the same number of tags

Stringbuilder htmltext = new stringbuilder ();

Try

{

Using (streamreader sr = new streamreader ("Path and name of the template page"))

{

String line;

While (line = Sr. Readline ())! = NULL)

{

Htmltext. append (line );

}

Sr. Close ();

}

}

Catch

{

Response. Write ("<SCRIPT> alert ('File Reading Error') </SCRIPT> ");

}

//---------------------Assign a value to the tag Array------------

Format [0] = "background = \" bg.jpg \"";//Background Image

Format [2] = "#990099 ";//Font color

Format [2] = "150px ";//Font Size

Format [3] = "<marquee>Generated templateHtmlPage</Marquee> ";//Text description

//----------ReplaceHtmMark the content you want to add

For (INT I = 0; I <4; I ++)

{

Htmltext. Replace ("$ htmlformat [" + I + "]", format [I]);

}

//----------GenerateHtmFile------------------――

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 conveniently generateHtmlFile. The program uses cyclic replacement, so it is very fast for templates that need to replace a large number of elements.

If you have any questions or errors, contact me.Jxf_yx@163.com

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.