asp.net to generate static pages and pagination code _ Practical Tips

Source: Internet
Author: User
Tags prev
1, static template page template.html, mainly defines a number of special characters, used to be replaced.
Copy Code code as follows:

<! 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> 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" > View more reviews </a> </form>
</div>
</body>

2, the Front state page newsadd.aspx, is a form, used to fill in the headlines and content of the news.
Copy Code code 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 ">
<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
Copy Code code 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 Button1_Click (object sender, EventArgs e)
{
String strdate = DateTime.Now.ToString ("yyMMdd") + "" + DateTime.Now.ToString ("Yyyymmddhhmmss");
String strFileName = Strdate + ". shtml";//stored in the database stockbests.cn
String strtitle=request.form["Title"]. ToString (). Trim ()//receive the headline from the pass.
String strcontent=request.form["Content"]. ToString (). Trim ()//Receive incoming content
string[] content = Strcontent.split (new char[])//split the contents and save to the array
int upbound = content. Upper bound of 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 = "Add success!" "; stockbests.cn
/**////////////////////////////Create the current date's folder start
String dir = Server.MapPath ("http://www.cnblogs.com/" + "newsfiles/" +datetime.now.tostring ("YyMMdd"));//to generate a folder
if (! Directory.Exists (dir))
{
Directory.CreateDirectory (dir);
}
/**////////////////////////////the end of the folder where the current date is created
Try
{
for (int i = 0; i < content. Length; i++)
{
string[] newcontent = new string[4];//definition and an array with a consistent number of HTML tags
StringBuilder strhtml = new StringBuilder ();
Creating StreamReader objects
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 ();
}
Assigning values to a tag array
Sqlserverdatabase db = new Sqlserverdatabase ();
DataSet ds = db. Select ("SELECT top 1 NewsId to Innews ORDER by NewsId DESC", null);/get ID
String strtable = "<table> <tr> <td>upurl </td> <td>number </td> <td>downurl & lt;/td> </tr> </table> "//down page table, note here Upurl (previous), number (page pagination), Downurl (Next page)
These three are used for substitution.
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 Paging 1,2,3 ...
for (int m = 1; M <=upbound; m++)
{
if (m = = 1)//If the first page is displayed 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 of the connection should be 20070524_2.shtml, etc.
Strnumber = Strnumber + "[" + "<a href=" + ". /"+ Strdate +" _ "+ N +". sHTML "+" > "+ M +" </a> "+"] ";
}
}
if (upbound = 0)//If there is no paging, directly save 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 to 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 show previous page
strtable = Strtable.replace ("Upurl", "");
if (I <= 1)//prev page pagination
strtable = Strtable.replace ("Upurl", "<a href=" + "...) /"+ Strdate +". sHTML "+ > Prev </a>");
Else
{
int p = i-1;
strtable = Strtable.replace ("Upurl", "<a href=" + "...) /"+ Strdate +" _ "+ P +". sHTML "+ > Prev </a>");
}
if (upbound==1)//If there is only one page, the page number is not displayed
Strnumber= "";
strtable = Strtable.replace ("number", "");
Else
strtable = Strtable.replace ("number", strnumber);//page replacement
/**/////////////////////////
if (i==upbound-1)//Last page does not display the next page
strtable = Strtable.replace ("Downurl", "");
if (i!= upbound-1)//Next page pagination
{
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 the form of open or write
using (FileStream fs = Finfo. Openwrite ())
{
Create a write stream based on the file flow created above
StreamWriter sw = New StreamWriter (FS, System.Text.Encoding.Default);
Write 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 ());
}
}
}
}
Related Article

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.