ASP. NET generate HTML pages (multiple types)

Source: Internet
Author: User

Asp.net generate HTML method 1

// Generate an HTML page
Public static bool writefile (string strtext, string strcontent, string strauthor)
{
String Path = httpcontext. Current. server. mappath ("/news /");
Encoding code = encoding. getencoding ("gb2312 ");
// Read the Template File
String temp = httpcontext. Current. server. mappath ("/news/text.html ");
Streamreader sr = NULL;
Streamwriter Sw = NULL;
String STR = "";
Try
{
Sr = new streamreader (temp, Code );
STR = Sr. readtoend (); // read the file
}
Catch (exception exp)
{
Httpcontext. Current. response. Write (exp. Message );
Httpcontext. Current. response. End ();
Sr. Close ();
}


String htmlfilename = datetime. Now. tostring ("yyyymmddhhmmss") + ". html ";
// Replace content
// At this time, the template file has been read into the variable named STR
STR = Str. Replace ("showarticle", strtext); // showarticle on the template page
STR = Str. Replace ("biaoti", strtext );
STR = Str. Replace ("content", strcontent );
STR = Str. Replace ("author", strauthor );
// Write an object
Try
{
Sw = new streamwriter (path + htmlfilename, false, Code );
Sw. Write (STR );
Sw. Flush ();
}
Catch (exception ex)
{
Httpcontext. Current. response. Write (ex. Message );
Httpcontext. Current. response. End ();
}
Finally
{
Sw. Close ();
}
Return true;

This function is stored in the conn. CS base class.
Reference in the Code for adding news: Project name: hover

If (hover. Conn. writefilethis. Title. Text. tostring), this. content. Text. tostring), this. Author. Text. tostring )))
{
Response. Write ("added successfully ");
}
Else
{
Response. Write ("An error occurred while generating HTML! ");
}
 

 

 

Private string createdetailpage (string eventid, string eventtitle, string eventbody, string eventtime, string eventstat)
{
// All template paths, template file names, generated file paths, and generated file names
String path, temp, htmlfilepath, htmlfilename;
Path = server. mappath ("");
Temp = server. mappath ("testhtml.htm ");
Htmlfilepath = path;
Htmlfilename = datetime. Now. tostring ("yyyymmddhhmmss") + datetime. Now. millisecond. tostring () + ". html ";

// Read Template
Encoding code = encoding. getencoding ("gb2312 ");

Streamreader sr = NULL;
Streamwriter Sw = NULL;
String STR = "";

Try
{
Sr = new streamreader (temp, Code );
STR = Sr. readtoend (); // read the file
}
Catch (exception exp)
{
Httpcontext. Current. response. Write ("<p> An error occurred while reading the file:" + exp. Message );
Httpcontext. Current. response. End ();
Sr. Close ();
}

// Replace content
// Modify the settings in the corresponding Template
STR = Str. Replace ("re_symbol_eventid", eventid );
STR = Str. Replace ("re_symbol_eventtitle", eventtitle );
STR = Str. Replace ("re_symbol_eventbody", eventbody );
STR = Str. Replace ("re_symbol_eventtime", eventtime );
STR = Str. Replace ("re_symbol_eventstat", eventstat );

// Write an object
Try
{
Sw = new streamwriter (htmlfilepath + "\" + htmlfilename, false, Code );
Sw. Write (STR );
Sw. Flush ();
}
Catch (exception ex)
{
Httpcontext. Current. response. Write ("<p> file write error:" + ex. Message );
Httpcontext. Current. response. End ();
}
Finally
{
Sw. Close ();
}
Return htmlfilename;
}

This is the case when calling:

// Retrieve the content. Here, I retrieve the data of the selected row in a gridview on the page.
Int I;
I = gridview1.selectedindex;
If (I = NULL | I =-1) I = 0;
String eventid, eventtitle, eventbody, eventtime, eventstat;
Eventid = gridview1.rows [I]. cells [0]. text;
Eventtitle = gridview1.rows [I]. cells [1]. text;
Eventbody = gridview1.rows [I]. cells [2]. text;
Eventtime = gridview1.rows [I]. cells [3]. text;
Eventstat = gridview1.rows [I]. cells [4]. text;

// Generate a file and return the file name
String FNA;
FNA = createdetailpage (eventid, eventtitle, eventbody, eventtime, eventstat );
Response. Write ("<p> file generated successfully:" + FNA );

Comparing the two methods, I think the second method is better than the first one, flexible, and fast to generate



Template page text.html code


Code:
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> showarticle </title>
<Body>
Biaoti
<Br>
Content <br>
Author
</Body>
</Html>
Biaoti
<Br>
Content <br>
Author
</Body>
</Html> Asp.net generate HTML Method 2:
   Ideas
1. Use tools such as DW-MX to generate HTML-format templates, and add special tags (for example, $ htmlformat $) where the formats need 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 the template, generate a new file name, and write it to the disk, write 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 a very complex page and add document to the JS file by using the method containing the JS file. 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 used here.
   Column code
   1、definition (template.htm) HTML template 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.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 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 ------------------――
  
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.

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.