Generate static pages under. net

Source: Internet
Author: User
Because the company's products are developed using ASP, a program was written with ASP some time ago to generate static pages and pagination, but due to. net Love, write this. net.

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> 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.

<% @ 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 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 ());
}
}
}
}

Do not directly copy and use it. The path in it must be changed, but the program is absolutely no problem. It has 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.

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.