Static Page. The first template is from the file, and the second template is read from the file.

Source: Internet
Author: User
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. net;
Using system. text;
Using system. IO;

Namespace tohtml
{
// 51aspx.com: generate a static page demo file. Keep this information for reprinting.
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{

}

// Generated based on the template and kept in the HTML folder (some source code is collected on the Network)
Protected void button#click (Object sender, eventargs E)
{
// The Source Code replaces the feature characters in the template.

String mbpath = server. mappath ("template.htm ");
Encoding code = encoding. getencoding ("gb2312 ");
Streamreader sr = NULL;
Streamwriter Sw = NULL;
String STR = NULL;

// Read
Try
{
Sr = new streamreader (mbpath, Code );
STR = Sr. readtoend ();

}
Catch (exception ex)
{
Throw ex;
}
Finally
{
Sr. Close ();
}

// Automatically rename the file according to the time, and the extension can be modified by yourself
String filename = datetime. Now. tostring ("yyyymmddhhmmss") + ". htm ";
STR = Str. Replace ("$ title $", txttitle. Text); // Replace the title
STR = Str. Replace ("$ content $", txtcontent. Text); // replace content

// Generate static files
Try
{
Sw = new streamwriter (server. mappath ("htm/") + filename, false, Code );
Sw. Write (STR );
Sw. Flush ();

}
Catch (exception ex)
{
Throw ex;
}
Finally
{
Sw. Close ();
Response. Write ("Congratulations <a href = htm/" + filename + "target = _ blank>" + filename + "</a> has been generated and saved in the HTM folder! ");
}

}

// Generate static page persistence based on the URL
Protected void button2_click (Object sender, eventargs E)
{
Encoding code = encoding. getencoding ("UTF-8 ");
Streamreader sr = NULL;
Streamwriter Sw = NULL;
String STR = NULL;

// Read the remote path
Webrequest temp = webrequest. Create (txturl. Text. Trim ());
Webresponse mytemp = temp. getresponse ();
Sr = new streamreader (mytemp. getresponsestream (), Code );
// Read
Try
{
Sr = new streamreader (mytemp. getresponsestream (), Code );
STR = Sr. readtoend ();

}
Catch (exception ex)
{
Throw ex;
}
Finally
{
Sr. Close ();
}
String filename = datetime. Now. tostring ("yyyymmddhhmmss") + ". htm ";

// Write
Try
{
Sw = new streamwriter (server. mappath ("htm/") + filename, false, Code );
Sw. Write (STR );
Sw. Flush ();

}
Catch (exception ex)
{
Throw ex;
}
Finally
{
Sw. Close ();
Response. Write ("Congratulations <a href = htm/" + filename + "target = _ blank>" + filename + "</a> has been generated and saved in the HTM folder! ");
}

}
}

}

 

**************************************** ***************************

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! This. ispostback)
{
Oledbconnection con = dB. getcon ();
Con. open ();
// Bind the news category
Oledbdataadapter ODR = new oledbdataadapter ("select * from type", con );
Dataset DS = new dataset ();
Datatable dt = new datatable ();

ODR. Fill (DS, "type"); // you can directly fill

Dt = Ds. Tables ["type"];

This. ddlnews. datasource = DT. defaultview;
This. ddlnews. datatextfield = "name ";
This. ddlnews. datavaluefield = "typeid ";
This. ddlnews. databind ();

// Bind a news Template
Oledbdataadapter odr1 = new oledbdataadapter ("select * From fileexample", con );
Dataset ds1 = new dataset ();
Odr1.fill (ds1, "fileexample ");
This. ddlmoban. datasource = ds1.tables ["fileexample"]. defaultview;
This. ddlmoban. datatextfield = "name ";
This. ddlmoban. datavaluefield = "ID ";
This. ddlmoban. databind ();
}
}
Protected void btn_add_click (Object sender, eventargs E)
{
String ddlnews = This. ddlnews. text;
String ddljb = This. ddljb. text;
String title = This. Title. Text. Trim ();
String ddlmoban = This. ddlmoban. text;
String itypeid = This. ddlnews. selectedvalue;
String iexampleid = This. ddlmoban. selectedvalue;
String content = This. content. Text. Trim ();
String time = this. Time. Text. Trim ();
String key = This. textbox1.text. Trim ();
String bianji = This. bianji. Text. Trim ();
If (this. Title. Text = "" | this. content. Text = "" | this. Time. Text = "")
{
Response. Write (@ "<script language = 'javascript '> window. Alert ('fill in the complete information! '); </SCRIPT> ");
Return;
}
Oledbconnection con = dB. getcon ();
Con. open ();
Oledbcommand cmd = new oledbcommand ("select e_memo from fileexample where id =" + iexampleid, con );

String moban = (string) cmd. executescalar (); // return the first query record. // read the template to a string.
Con. Close ();

// Add the content to the template
Moban = moban. Replace ("t_title", title); // page title = article title
Moban = moban. Replace ("t_time", time );
Moban = moban. Replace ("t_keywords", key );
Moban = moban. Replace ("t_author", bianji );
Moban = moban. Replace ("t_editor", bianji );
Moban = moban. Replace ("t_content", content );
Moban = moban. Replace ("t_typeid", iexampleid. tostring ());

// Create a folder named by the current date
String pathname = datetime. Now. tostring ("yyyy-mm-dd ");

String fullpath = "~ /Html/"+ pathname; // Add the directory name

If (! System. Io. Directory. exists (fullpath) // if the current file does not exist
{
Directory. createdirectory (server. mappath (fullpath ));
}


// Generate an HTML file
String Path = httpcontext. Current. server. mappath (fullpath + "/"); // save the file
String filename = datetime. Now. tostring ("yyyymmddhhmmss") + ". html"; // generate a file name with a suffix html
Encoding code = encoding. getencoding ("gb2312 ");

Streamwriter Sw = NULL;
Try
{
Sw = new streamwriter (path + filename, false, Code );
Sw. Write (moban );
Sw. Flush ();

}

Catch (exception ex)
{
Httpcontext. Current. response. Write (ex. Message );
Httpcontext. Current. response. End ();
}

Finally
{
Sw. Close ();
}
Try
{
String url = pathname + "/" + filename;
Oledbconnection conn = dB. getcon ();
Conn. open ();
String SQL = "insert into filelist (typeid, title, author, URL, limit, inputtime, editor, keywords) values (" + itypeid + ", '" + title + "', '"+ bianji +"', '"+ URL +"', '"+ ddljb +"', '"+ time +"', '"+ bianji + "', '"+ key + "')";
Oledbcommand cmd1 = new oledbcommand (SQL, Conn );
Statement 1.executenonquery ();
Conn. Close ();
Response. Write (@ "<script language = 'javascript '> window. Alert (' added successfully! ') </SCRIPT> ");
Response. Redirect ("newslist. aspx ");
}
Catch (exception ex)
{
Response. Write ("error, error cause:" + ex. Message );
Response. End ();
}

}
// Format html
Private string formatstr (string Str)
{
String content = Str. Replace ("<", "& lt ;");
Content = content. Replace (">", "& gt ;");
// Content = content. Replace (CHR (13), "<br> ");
Content = content. Replace ("" r "," <br> ");
Content = content. Replace ("", "& nbsp ;");
Content = content. Replace ("[isok]", "Content = content. Replace ("[B]", "<B> ");
Content = content. Replace ("[Red]", "<font color = cc0000> ");
Content = content. Replace ("[Big]", "<font size = 7> ");
Content = content. Replace ("[/isok]", "> </img> ");
Content = content. Replace ("[/B]", "</B> ");
Content = content. Replace ("[/Red]", "</font> ");
Content = content. Replace ("[/big]", "</font> ");
Return content;
}
}

 

 

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.