1. The static template page template.html mainly defines some special characters for replacement.
Copy codeThe Code is as follows:
<! 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> 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 = "http://www.dwww.cn/News/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.
Copy codeThe Code is as follows:
<% @ 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
Copy codeThe Code is as follows:
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.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 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.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 ());
}
}
}
}