⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 creathtml.aspx.cs

📁 asp.net专家200问(含源代码解决法案
💻 CS
字号:
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 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </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标记数目一致的数组
			StringBuilder strhtml = new StringBuilder();
			try 
			{
				//创建StreamReader对象
				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());
			}
			//为标记数组赋值
			newContent[0] = txtTitle.Text;//标题
			newContent[1] = "BackColor='#cccfff'";//背景色
			newContent[2] = "#ff0000";//字体颜色
			newContent[3] = "100px";//字体大小
			newContent[4] = txtContent.Text;//主要内容

			//根据上面新的内容生成html文件
			try
			{
				//指定要生成的HTML文件
				string fname = Server.MapPath("createHTML") +"\\" + DateTime.Now.ToString("yyyymmddhhmmss") + ".html";
				//替换html模版文件里的标记为新的内容
				for(int i=0;i < 5;i++)
				{
					strhtml.Replace("$htmlkey["+i+"]",newContent[i]);
				}
				//创建文件信息对象
				FileInfo finfo = new FileInfo(fname);
				//以打开或者写入的形式创建文件流
				using(FileStream fs = finfo.OpenWrite())
				{
					//根据上面创建的文件流创建写数据流
					StreamWriter sw = new StreamWriter(fs,System.Text.Encoding.GetEncoding("GB2312"));
					//把新的内容写到创建的HTML页面中
					sw.WriteLine(strhtml);
					sw.Flush();
					sw.Close();
				}
				//设置超级链接的属性
				hyCreateFile.Text = DateTime.Now.ToString("yyyymmddhhmmss")+".html";
				hyCreateFile.NavigateUrl = "createHTML/"+DateTime.Now.ToString("yyyymmddhhmmss")+".html";
			}
			catch(Exception err)
			{ 
				Response.Write (err.ToString());
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -