asp.net to generate static page implementation Method _ Practical skills

Source: Internet
Author: User

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;//Database Operations class
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 strtitle=request.form["Title"]. ToString ();
String strcontent=request.form["Content"]. ToString ();
Sqlserverdatabase db = new Sqlserverdatabase ();
BOOL success = db. Insert ("INSERT into innews (title,content) VALUES (' + strtitle +" ', ' "+ strcontent + ')", null);
if (success)
Message.Text = "Add success!" ";
/**////////////////////////////Create the current date's folder start
String dir = Server.MapPath ("..."). /.. /"+" newsfiles/"+datetime.now.tostring (" YyMMdd "));
if (! Directory.Exists (dir))
{
Directory.CreateDirectory (dir);
}
/**////////////////////////////the end of the folder where the current date is created

string[] newcontent = new string[5];//definition and an array with a consistent number of HTML tags
StringBuilder strhtml = new StringBuilder ();
Try
{
Creating StreamReader objects
using (StreamReader sr = new StreamReader (Server.MapPath) (". /.. /"+" newsfiles/") +"/template.html ")
{
String oneline;
Read the specified HTML file template
while (Oneline = Sr. ReadLine ())!= null)
{
strHTML. Append (oneline);
}
Sr. Close ();
}
}
catch (Exception err)
{
Output exception information
Response.Write (Err. ToString ());
}
Assigning values to a tag array
Newcontent[0] = strtitle;//title
NEWCONTENT[1] = "backcolor= ' #cccfff '";//Background color
NEWCONTENT[2] = "#ff0000";//Font Color
NEWCONTENT[3] = "100px";//font size
NEWCONTENT[4] = strcontent;//main content

Generate HTML files based on the new content above
Try
{
Specify the HTML file to generate
String fname = Server.MapPath (".. /.. /"+" newsfiles/"+ DateTime.Now.ToString (" YyMMdd ")) +"/"+ DateTime.Now.ToString (" YYYYMMDDHHMMSS ") +". html ";
Replace the markup in the HTML template file with the new content
for (int i = 0; i < 5; i++)
{
strHTML. Replace ("$htmlkey [" + i + "]", newcontent[i]);
}
Create a file information object
FileInfo finfo = new FileInfo (fname);
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.GetEncoding ("GB2312"));
Write new content to the HTML page you created
Sw. WriteLine (strhtml);
Sw. Flush ();
Sw. Close ();
}
}
catch (Exception err)
{
Response.Write (Err. ToString ());
}
}
}
}


template.html Code
Copy Code code as follows:



<title> $htmlkey [0]</title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<body >
<table $htmlkey [1] height= "100%" border= "0" width= "100%" cellpadding= "ten" cellspacing= "0" bgcolor= "#eeeeee" style= " border:1px solid #000000 "mce_style=" border:1px solid #000000 ">
<tr>
&LT;TD width= "100%" valign= "Middle" align= "left" >
<span style= "COLOR: $htmlkey [2];font-size: $htmlkey [3]" mce_style= "color: $htmlkey [2];font-size: $htmlkey [3]" > $htmlkey [4]</span>
</td>
</tr>
</table>
</body>

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.