The main principle is to replace the special characters in the template.
1. The static template page template.html mainly defines some special characters for replacement.
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> $ title </title>
</Head>
<Body>
<Div style = "width: pixel PX; Height: 54px" align = "center">
<Br/>
$ Title </div>
<Div style = "width: pixel PX; Height: 8px">
Browse <font color = "red"> <SCRIPT src = "http: // localhost/. Net/newsfiles/clickcount. aspx? Newsid = $ newsid "> </SCRIPT> </font> times $ time </div>
<Div style = "width: pixel PX; Height: 100px">
$ Content </div>
<Div style = "width: 416px; Height: 9px">
$ Pager </div>
<Div style = "width: 416px; Height: 8px">
<Form ID = "form1" Action = ".../addcomment. aspx" style = "margin: 0px">
<Input id = "text1" type = "text"/> <br/>
<Textarea id = "commentcontent" Cols = "20" rows = "2"> </textarea>
<Br/>
<Input id = "newsid" type = "hidden" value = "$ newsid"/>
<Input id = "button1" type = "Submit" value = "button"/>
<A href = ".../display. aspx? Newsid = $ newsid "> View more comments </a> </form>
</Div>
</Body>
</Html>
2. newsadd. aspx is a form used to fill in the news title and content.
<%... @ Page Language = "C #" autoeventwireup = "false" validaterequest = "false" codefile = "newsadd. aspx. CS "inherits =" newsadd. admin_adminpanel_newsadd "%>
<%... @ Register tagprefix = "fckeditorv2" namespace = "fredck. fckeditorv2" assembly = "fredck. fckeditorv2" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Add news </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: Label id = "label2" runat = "server" text = "title"> </ASP: Label>
<Asp: textbox id = "title" runat = "server" width = "325px"> </ASP: textbox> <br/>
<Asp: Label id = "label1" runat = "server" text = "content"> </ASP: Label>
<Fckeditorv2: FCKeditor id = "content" basepath = "~ /FCKeditor/"runat =" server "Height =" 400px "width =" 70% "> </fckeditorv2: FCKeditor>
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "button"/>
<Asp: Label id = "message" runat = "server"> </ASP: Label> </div>
</Form>
</Body>
</Html>
3. newsadd. aspx. CS
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
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 mysqlserver;
Using system. IO;
Using system. text;
Namespace newsadd
...{
Public partial class admin_adminpanel_newsadd: system. Web. UI. Page
...{
Protected void page_load (Object sender, eventargs E)
...{
}
Protected void button#click (Object sender, eventargs E)
...{
String strdate = datetime. Now. tostring ("yymmdd") + "\" + datetime. Now. tostring ("yyyymmddhhmmss ");
String strfilename = strdate + ". shtml"; // store it in the database
String strtitle = request. Form ["title"]. tostring (). Trim (); // receives the uploaded title.
String strcontent = request. Form ["content"]. tostring (). Trim (); // receives the transmitted content.
String [] content = strcontent. Split (New char []... {'|'}); // splits the content and saves it to an array.
Int upbound = content. length; // upper limit of the array
Sqlserverdatabase DB = new sqlserverdatabase ();
Bool success = dB. insert ("insert into innews (title, content, filepath) values ('" + strtitle + "', '" + strcontent + "', '" + strfilename + "') ", null );
// If (SUCCESS)
// Message. Text = "added successfully! ";
/** // Start when the folder on the current date is created
String dir = server. mappath ("http://www.cnblogs.com/" + "newsfiles/" + datetime. Now. tostring ("yymmdd"); // used to generate folders
If (! Directory. exists (DIR ))
...{
Directory. createdirectory (DIR );
}
/** // The folder created on the current date ends.
Try
...{
For (INT I = 0; I <content. length; I ++)
...{
// String [] newcontent = new string [4]; // defines an array with the same number of HTML tags
Stringbuilder strhtml = new stringbuilder ();
// Create a streamreader object
Using (streamreader sr = new streamreader (server. mappath ("http://www.cnblogs.com/" + "newsfiles/") + "\ template.html", encoding. getencoding ("gb2312 ")))
...{
String oneline;
// Read the specified HTML file template
While (oneline = Sr. Readline ())! = NULL)
...{
Strhtml. append (oneline );
}
Sr. Close ();
}
// Assign values to the marked Array
// Sqlserverdatabase DB = new sqlserverdatabase ();
Dataset DS = dB. Select ("select top 1 newsid from innews order by newsid DESC", null); // obtain the ID
String strtable = "<Table> <tr> <TD> $ upurl </TD> <TD> $ number </TD> <TD> $ downurl </TD> </tr> </table> "; // The top and bottom pages. Pay attention to the $ upurl (Previous Page), $ number (page number), and $ downurl (next page)
// These three are used for replacement.
String filepath = "";
Strhtml = strhtml. Replace ("$ title", strtitle );
Strhtml = strhtml. Replace ("$ newsid", DS. Tables [0]. Rows [0] ["newsid"]. tostring ());
Strhtml = strhtml. Replace ("$ time", datetime. Now. tostring ("yyyy/mm/DD "));
Strhtml = strhtml. Replace ("$ content", content [I]);
String strnumber = ""; // The number is displayed on pages 1, 2, 3 ......
For (int m = 1; m <= upbound; m ++)
...{
If (M = 1) // This is displayed on the first page: 20070524.shtmlbut not 20070524_1.shtml
Strnumber = strnumber + "[" + "<a href =" + ".. /"+ strdate + ". shtml "+"> "+ M +" </a> "+"] ";
Else
...{
Int n = m-1; // The connection to the third page should be 20070524_2.shtml, and so on.
Strnumber = strnumber + "[" + "<a href =" + ".. /"+ strdate +" _ "+ N + ". shtml "+"> "+ M +" </a> "+"] ";
}
}
If (upbound = 0) // if there are no pages, save them by date and time.
...{
Filepath = server. mappath ("http://www.cnblogs.com/") + "newsfiles" + "//" + strdate + ". shtml ";
Strhtml = strhtml. Replace ("$ pager ","");
}
Else // press 20070524.shtml1_20070524_1.shtml to save the negative effect.
...{
If (I = 0)
Filepath = server. mappath ("http://www.cnblogs.com/") + "newsfiles" + "//" + strdate + ". shtml ";
Else
Filepath = server. mappath ("http://www.cnblogs.com/") + "newsfiles" + "//" + strdate + "_" + I + ". shtml ";
If (I = 0) // the previous page is not displayed on the first page
Strtable = strtable. Replace ("$ upurl ","");
If (I <= 1) // the previous page
Strtable = strtable. replace ("$ upurl", "<a href =" + ".. /"+ strdate + ". shtml "+"> previous page </a> ");
Else
...{
Int P = I-1;
Strtable = strtable. replace ("$ upurl", "<a href =" + ".. /"+ strdate +" _ "+ P + ". shtml "+"> previous page </a> ");
}
If (upbound = 1) // if there is only one page, no page number is displayed.
// Strnumber = "";
Strtable = strtable. Replace ("$ number ","");
Else
Strtable = strtable. Replace ("$ number", strnumber); // Replace the page number
/**/////////////////////////
If (I = upbound-1) // The last page does not show the next page
Strtable = strtable. Replace ("$ downurl ","");
If (I! = Upbound-1) // next page
...{
Int q = I + 1;
Strtable = strtable. replace ("$ downurl", "<a href =" + ".. /"+ strdate +" _ "+ q + ". shtml "+"> next page </a> ");
}
Else
...{
Int J = upbound-1;
Strtable = strtable. replace ("$ downurl", "<a href =" + ".. /"+ strdate +" _ "+ J + ". shtml "+"> next page </a> ");
}
Strhtml = strhtml. Replace ("$ pager", strtable );
}
// Create a file information object --------------------------------------------
Fileinfo finfo = new fileinfo (filepath );
// Create a file stream in the form of opening or writing
Using (filestream FS = finfo. openwrite ())
...{
// Create a write data stream based on the file stream created above
Streamwriter Sw = new streamwriter (FS, system. Text. encoding. Default );
// Write the new content to the created HTML page
Sw. writeline (strhtml );
Sw. Flush ();
Sw. Close ();
}
}
}
Catch (exception ERR)
...{
// Output exception information
Response. Write (ERR. tostring ());
}
}
}
}
Do not copy the file directly. The path in the file must be changed,ProgramAbsolutely no problem. I have passed the test locally.
In addition, for example, if I want to divide the news content into four pages, we should write as follows: 111 | 222 | 333 | 444.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/kbh1983/archive/2007/11/01/1860859.aspx