1. Simple generation of htm page code without paging
The code is as follows: |
Copy code |
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.html controls; Using mysqlserver; // database operation class 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 strTitle = Request. Form ["Title"]. ToString (); String strContent = Request. Form ["Content"]. ToString (); SqlServerDataBase db = new SqlServerDataBase (); Bool success = db. Insert ("insert into inNews (Title, Content) values ('" + strTitle + "', '" + strContent + "')", null ); // If (success) // Message. Text = "added successfully! "; /** // Start the creation of the folder on the current date String dir = Server. MapPath (".../" + "NewsFiles/" + DateTime. Now. ToString ("yyMMdd ")); If (! Directory. Exists (dir )) { Directory. CreateDirectory (dir ); } /** // End of the folder on the current creation date String [] newContent = new string [5]; // defines an array with the same number of html tags StringBuilder strhtml = new StringBuilder (); Try { // Create a StreamReader object Using (StreamReader sr = new StreamReader (Server. MapPath (".../../" + "NewsFiles/") + "/template.html ")) { String oneline; // Read the specified HTML file Template While (oneline = sr. ReadLine ())! = Null) { Strhtml. Append (oneline ); } Sr. Close (); } } Catch (Exception err) { // Output exception information Response. Write (err. ToString ()); } // Assign values to the marked array NewContent [0] = strTitle; // title NewContent [1] = "BackColor = '# cccfff'"; // background color NewContent [2] = "# ff0000"; // font color NewContent [3] = "100px"; // font size NewContent [4] = strContent; // Main content // Generate an html file based on the new content above Try { // Specify the HTML file to be generated String fname = Server. mapPath (".. /.. /"+" NewsFiles/"+ DateTime. now. toString ("yyMMdd") + "/" + DateTime. now. toString ("yyyymmddhhmmss") + ". html "; // Replace the new content in the html template file For (int I = 0; I <5; I ++) { Strhtml. Replace ("$ htmlkey [" + I + "]", newContent [I]); } // Create a file information object FileInfo finfo = new FileInfo (fname ); // 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. GetEncoding ("GB2312 ")); // Write the new content to the created HTML page Sw. WriteLine (strhtml ); Sw. Flush (); Sw. Close (); } } Catch (Exception err) { Response. Write (err. ToString ()); } } } } |
2. Template.html
The code is as follows: |
Copy code |
<Html> <Head> <Title> $ htmlkey [0] </title> <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"> </Head> <Body> <Table $ htmlkey [1] height = "100%" border = "0" width = "100%" cellpadding = "10" cellspacing = "0" bgcolor = "# eeeeee" style = "border: 1px solid #000000 "mce_style =" border: 1px solid #000000 "> <Tr> <Td width = "100%" valign = "middle" align = "left"> <Span style = "color: $ htmlkey [2]; font-size: $ htmlkey [3]" mce_style = "color: $ htmlkey [2]; font-size: $ htmlkey [3] "> $ htmlkey [4] </span> </Td> </Tr> </Table> </Body> </Html> |
The code above does not have the paging function. If you want to pagination, we need to perform some processing.
1. Static template page template.html, which defines some special characters for replacement.
The code is as follows: |
Copy code |
<! 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> asp.net: generate static page _ www.111cn.net </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> 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 = "/News/Display. aspx? NewsId = NewsId "> View More comments </a> </form> </Div> </Body> </Html>
|
2. NewsAdd. aspx
A form that is used to enter the news title and content.
The code is as follows: |
Copy code |
<% @ 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_www.111cn.net </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
The code is as follows: |
Copy code |
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 stockbests.cn 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! "; Stockbests.cn /** // Start when the folder on the current date is created String dir = Server. MapPath ("http://www.111cn.net/" + "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.111cn.net/" + "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 upper and lower Pages. Note 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.111cn.net/") + "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.111cn.net/") + "NewsFiles" + "//" + strDate + ". shtml "; Else FilePath = Server. MapPath ("http://www.111cn.net/") + "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 ()); } } } } |
Ps: If you want to add a next hop box, you can use for operations.
The code is as follows: |
Copy code |
For (int j = 1; j <= (pageTotal + 1); j ++) { If (j = 1) { Sb. Append ("<option value = '" + absolutePaths + "\" + name + ". html"> "+ j +" </option> "); } Else { Sb. append ("<option value = '" + absolutePaths + "\" + name + "-" + (j-1) + ". html '> "+ j +" </option> "); } } |
In this way, we will not discuss it.