?? creathtml.aspx.cs
字號(hào):
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
namespace CommonFunction
{
/// <summary>
/// CreatHtml 的摘要說明。
/// </summary>
public class CreatHtml : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtContent;
protected System.Web.UI.WebControls.HyperLink hyCreateFile;
protected System.Web.UI.WebControls.TextBox txtTitle;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Button btnCreate;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
}
#region Web 窗體設(shè)計(jì)器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計(jì)器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.btnCreate.Click += new System.EventHandler(this.btnCreate_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnCreate_Click(object sender, System.EventArgs e)
{
string[] newContent = new string[5];//定義和html標(biāo)記數(shù)目一致的數(shù)組
StringBuilder strhtml = new StringBuilder();
try
{
//創(chuàng)建StreamReader對(duì)象
using (StreamReader sr = new StreamReader(Server.MapPath("createHTML") + "\\template.html"))
{
String oneline;
//讀取指定的HTML文件模板
while ((oneline = sr.ReadLine()) != null)
{
strhtml.Append(oneline);
}
sr.Close();
}
}
catch(Exception err)
{
//輸出異常信息
Response.Write(err.ToString());
}
//為標(biāo)記數(shù)組賦值
newContent[0] = txtTitle.Text;//標(biāo)題
newContent[1] = "BackColor='#cccfff'";//背景色
newContent[2] = "#ff0000";//字體顏色
newContent[3] = "100px";//字體大小
newContent[4] = txtContent.Text;//主要內(nèi)容
//根據(jù)上面新的內(nèi)容生成html文件
try
{
//指定要生成的HTML文件
string fname = Server.MapPath("createHTML") +"\\" + DateTime.Now.ToString("yyyymmddhhmmss") + ".html";
//替換html模版文件里的標(biāo)記為新的內(nèi)容
for(int i=0;i < 5;i++)
{
strhtml.Replace("$htmlkey["+i+"]",newContent[i]);
}
//創(chuàng)建文件信息對(duì)象
FileInfo finfo = new FileInfo(fname);
//以打開或者寫入的形式創(chuàng)建文件流
using(FileStream fs = finfo.OpenWrite())
{
//根據(jù)上面創(chuàng)建的文件流創(chuàng)建寫數(shù)據(jù)流
StreamWriter sw = new StreamWriter(fs,System.Text.Encoding.GetEncoding("GB2312"));
//把新的內(nèi)容寫到創(chuàng)建的HTML頁面中
sw.WriteLine(strhtml);
sw.Flush();
sw.Close();
}
//設(shè)置超級(jí)鏈接的屬性
hyCreateFile.Text = DateTime.Now.ToString("yyyymmddhhmmss")+".html";
hyCreateFile.NavigateUrl = "createHTML/"+DateTime.Now.ToString("yyyymmddhhmmss")+".html";
}
catch(Exception err)
{
Response.Write (err.ToString());
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -