Aspx Dynamic HTML generation (2)

Source: Internet
Author: User

Method 1: directly convert a page

Public static bool createlist (string URL, string FNA) {bool OK; // prepare to generate string strhtml; streamreader sr = NULL; // used to read stream streamwriter Sw = NULL; // used to write the file encoding code = encoding. getencoding ("UTF-8"); // defines the encoding // construct a Web request, send a request, and obtain the response webrequest httpwebrequest = NULL; webresponse httpwebresponse = NULL; httpwebrequest = webrequest. create (URL); httpwebresponse = httpwebrequest. getresponse (); // get the stream sr = new streamreader (httpwebresponse. getresponsestream (), Code); strhtml = sr. readtoend (); // write the file try {Sw = new streamwriter (FNA, false, Code); Sw. write (strhtml); Sw. flush (); OK = true;} catch (exception ex) {httpcontext. current. response. write ("<p> file writing error:" + ex. message); httpcontext. current. response. end (); OK = false;} finally {SW. close ();} Return OK ;}

Call

String url = @ "http: // localhost: 15598/OA/publicgv. aspx? Id = 14 "; // html page file name string FNA = server. mappath ("html") + "\" + datetime. now. tostring ("yyyymmddhhmmss") + datetime. now. millisecond. tostring () + ". html "; if (aspxtohtml. createlist (URL, FNA) {response. write ("<p> file generated successfully:" + FNA );}

The second method is to use an HTML template to generate an HTML page. The template contains the corresponding tag, which can be used to retrieve data from the database and other places. Fill in this tag to generate an HTML page, this method is useful in many news systems.

Private string createdetailpage (string eventid, string eventtitle, string eventbody, string eventtime, string eventstat) {// All templates path, template file name, generated file path, generated file name 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 the content // modify STR = STR for the settings in the corresponding template. 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 the file try {Sw = new streamwriter (htmlfilepath + "//" + htmlfilename, false, Code); Sw. write (STR); Sw. flush ();} catch (exception ex) {httpcontext. current. response. write ("<p> file writing error:" + ex. message); httpcontext. current. response. end ();} finally {SW. close () ;}return htmlfilename ;}

This is the case when calling:

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 the file, and return the file name string FNA; FNA = createdetailpage (eventid, eventtitle, eventbody, eventtime, eventstat); response. write ("<p> file generated successfully:" + FNA );

The previous figure.

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.