Asp.net is the easiest way to generate static pages (source code)

Source: Internet
Author: User

Public string IssuanceHTML (string NC_Id_S)

{

String returnValue = "";

String htmlfilename = DateTime. Now. ToString ("yyyyMMddHHmmss") + ". html ";

SqlConnection conn = null;

SqlDataReader sdr = null;

Try

{

String [] NC_Id_SS = NC_Id_S.Split (','); // Split data during batch operations

For (int I = 0; I <NC_Id_SS.Length; I ++) // cyclically generated

{Conn = Connection. CreateDbConn ();

Conn. Open ();

SqlCommand cmd = new SqlCommand ();

Cmd. Connection = conn;

Cmd. CommandText = "SELECT NC_Name_S, NC_Content_S FROM NewsContent WHERE NC_Id_S = '" + NC_Id_S + "'";

Sdr = cmd. ExecuteReader ();

Sdr. Read ();

String path = HttpContext. Current. Server. MapPath ("/HTML/"); // generate the file path

Encoding code = Encoding. GetEncoding ("gb2312"); // Encoding rules

String temp = HttpContext. Current. Server. MapPath ("/Templet/C8Article.html"); Template File

StreamReader sr = null;

StreamWriter sw = null;

String str = "";

Sr = new StreamReader (temp, code );

Str = sr. ReadToEnd ();

Sr. Close ();

Str = str. Replace ("spagetitle", sdr. GetString (0). ToString (); // spagetitle is the content of the template file.

Str = str. Replace ("scontent", sdr. GetString (1). ToString ());

Sw = new StreamWriter (path + htmlfilename, false, code );

Sw. Write (str );

Sw. Flush ();

Sw. Close ();

ReturnValue = "yes ";

}

Catch (Exception e)

{ReturnValue = e. Message. ToString ();

}

Finally

{Sdr. Close ();

Conn. Close ();

}

Return returnValue;

}

Bytes --------------------------------------------------------------------------------------------
Another Code
Bytes --------------------------------------------------------------------------------------------
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 ";
String strTitle = Request. Form ["Title"]. ToString (). Trim ();
String strContent = Request. Form ["Content"]. ToString (). Trim ();
String [] content = strContent. Split (new Char []... {'content '});
Int upbound = content. Length;
SqlServerDataBase db = new SqlServerDataBase ();
Bool success = db. insert ("insert into inNews (Title, Content, FilePath) values ('" + strTitle + "', '" + strContent + "', '" + strFileName + "') ", null );

String dir = Server. MapPath ("http://www.cnblogs.com/" + "NewsFiles/" + DateTime. Now. ToString ("yyMMdd "));
If (! Directory. Exists (dir ))
{
Directory. CreateDirectory (dir );
}

Try
{
For (int I = 0; I <content. Length; I ++)
{
// String [] newContent = new string [4];
StringBuilder strhtml = new StringBuilder ();
Using (StreamReader sr = new StreamReader (Server. MapPath ("http://www.cnblogs.com/" + "NewsFiles/") + "\ template.html", Encoding. GetEncoding ("gb2312 ")))
{
String oneline;
While (oneline = sr. ReadLine ())! = Null)
{
Strhtml. Append (oneline );
}
Sr. Close ();
}
DataSet ds = db. Select ("select top 1 NewsId from inNews order by NewsId desc", null );
String strTable = "<table> <tr> <td> $ upUrl </td> <td> $ Number </td> <td> $ downUrl </td> </tr> </table> ";
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 = "";
For (int m = 1; m <= upbound; m ++)
{
If (m = 1)
StrNumber = strNumber + "[" + "<a href =" + ".. /"+ strDate + ". shtml "+"> "+ m +" </a> "+"] ";
Else
{
Int n = m-1;
StrNumber = strNumber + "[" + "<a href =" + ".. /"+ strDate +" _ "+ n + ". shtml "+"> "+ m +" </a> "+"] ";
}
}
If (upbound = 0)
{
FilePath = Server. MapPath ("http://www.cnblogs.com/") + "NewsFiles" + "//" + strDate + ". shtml ";
Strhtml = strhtml. Replace ("$ Pager ","");
}
Else
{
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)
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)
StrTable = strTable. Replace ("$ Number ","");
Else
StrTable = strTable. Replace ("$ Number", strNumber );
If (I = upbound-1)
StrTable = strTable. Replace ("$ downUrl ","");

If (I! = Upbound-1)
{
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 );
}
FileInfo finfo = new FileInfo (FilePath );
Using (FileStream fs = finfo. OpenWrite ())
{
StreamWriter sw = new StreamWriter (fs, System. Text. Encoding. Default );
Sw. WriteLine (strhtml );
Sw. Flush ();
Sw. Close ();
}
}
}
Catch (Exception err)
{
Response. Write (err. ToString ());
}
}
}
}

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.