. NET generating static pages and paging

Source: Internet
Author: User

Because the company's products with ASP development, a period of time with ASP to write a static page and paging program, but due to the love of. NET, wrote this. NET to generate static pages and pagination of the program.

The main principle is to replace the special characters in the template.

1, static template page template.html, mainly defines some special characters, used to be replaced.

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title>Title</title>
<body>
<div style= "WIDTH:417PX; height:54px "align=" Center >
<br/>
Title</div>
<div style= "WIDTH:417PX; Height:8px ">
Browse <font color= "Red" ><script src= "http://localhost/.Net/NewsFiles/ClickCount.aspx? Newsid=newsid "></script></font> Times time</div>
<div style= "WIDTH:417PX; 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= "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" > see more Comments </a></form>
</div>
</body>

2, the Pre-state page newsadd.aspx, is a form, used to fill in the title and content of the news.

<%@ 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

<title> Add News </title>
<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= "Button1_Click" text= "button"/>
<asp:label id= "Message" runat= "Server" ></asp:Label></div>
</form>
</body>

3. Background page 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 Button1_Click (object sender, EventArgs e)
{
String strdate = DateTime.Now.ToString ("yyMMdd") + "\" + DateTime.Now.ToString ("Yyyymmddhhmmss");
String strFileName = Strdate + ". shtml";//Store in database
String strtitle=request.form["Title"]. ToString (). Trim ();//receive the title of the pass-over
String strcontent=request.form["Content". ToString (). Trim ();//Receive transmitted content
string[] content = Strcontent.split (new char[] {' | '}); /split the content and save to the array
int upbound = content. The upper bound of the length;//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 the folder where 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 end of the folder that created the current date
Try
{
for (int i = 0; i < content. Length; i++)
{
string[] newcontent = new string[4];//definition and 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;
Reads the specified HTML file template
while (Oneline = Sr. ReadLine ()) = null)
{
strHTML. Append (oneline);
}
Sr. Close ();
}

Assigning values to tagged arrays
Sqlserverdatabase db = new Sqlserverdatabase ();
DataSet ds = db. Select ("SELECT top 1 NewsId from Innews ORDER by NewsId DESC", null);//Get ID
String strtable = "<table><tr><td>upurl</td><td>number</td><td>downurl </td></tr></table> ";///page table, notice here Upurl (previous page), number (page pagination), Downurl (Next page)
These three are 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 = "";//Digital pagination ...
for (int m = 1; M <=upbound; m++)
{
if (m = = 1)//If it is the first page is shown in this way: 20070524.shtml instead of 20070524_1.shtml
Strnumber = Strnumber + "[" + "<a href=" + "... /"+ Strdate +". sHTML "+" > "+ M +" </a> "+"] ";
Else
{
int n = m-1;//The third page connection should be 20070524_2.shtml, and so on
Strnumber = Strnumber + "[" + "<a href=" + "... /"+ Strdate +" _ "+ N +". sHTML "+" > "+ M +" </a> "+"] ";
}
}
if (Upbound = = 0)//If there is no paging, save directly by date time
{
FilePath = Server.MapPath ("http://www.cnblogs.com/") + "Newsfiles" + "//" + Strdate + ". shtml";
strHTML = strhtml. Replace ("Pager", "" ");
}
else//otherwise press 20070524.shtml, 20070524_1.shtml this effect save
{
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)//first page does not display the previous page
strtable = Strtable.replace ("Upurl", "" ");

if (i <= 1)//prev 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, the page number
//strnumber= "" is not displayed;
strtable = Strtable.replace ("number", "");
Else
strtable = Strtable.replace ("number", strnumber);//page numbers replace
/**/////////////////////////
if (i== UPBOUND-1)//Last page does not display the next page
Strtable = strtable.replace ("Downurl", "");

if (i! = upbound-1)//Next Page page
{
int q = i + 1;
strtable = Strtable.replace ("Downurl", "<a href=" + "...) /"+ Strdate +" _ "+ Q +". sHTML "+" > Next </a> ");
}
Else
{
int j = upbound-1;
strtable = Strtable.replace ("Downurl", "<a href=" + "...) /"+ Strdate +" _ "+ j +". sHTML "+" > Next </a> ");
}

strHTML = strhtml. Replace ("Pager", strtable);
}
Create a file information object--------------------------------------------
FileInfo finfo = new FileInfo (FilePath);
Create a file stream in open or write form
using (FileStream fs = Finfo. OpenWrite ())
{
Create a write stream based on the file stream created above
StreamWriter SW = new StreamWriter (FS, System.Text.Encoding.Default);
Write the new content to the HTML page you created
Sw. WriteLine (strhtml);
Sw. Flush ();
Sw. Close ();
}
}
}
catch (Exception err)
{
Output exception information
Response.Write (Err. ToString ());
}
}
}
}

Please do not directly copy the use, the inside of the path needs to be changed, but the program absolutely no problem, in my local has been tested pass.

In addition, when using, for example, I want to divide the content of the news into 4 pages, it should be written like this: 111|222|333|444.

. NET generating static pages and paging

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.