您的位置:首页 > 编程语言 > ASP

asp.net自动生成html页面

2009-03-13 17:44 731 查看
如何让asp.net自动生成html页面之一(c#语言)
2007年09月16日 星期日 01:01 A.M.

首先创建一个名为“aspnettrancehtml”asp.netWeb解决方案;

其次,创建一个名为TextModel.html模版,效果如下:
注意标签的格式

$Biaoti$
作者:$Author$ 发布日期:$WriterTime$
$Content$
2007版权所有
最后,就要涉及到编程了,创建一个名为aspnettohtml.aspx页面,aspnettohtml.aspx.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 aspnettrancehtml
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;

StreamReader sr;
StreamWriter sw ;
protected System.Web.UI.WebControls.TextBox tb_content;//接受文章详情的textbox
protected System.Web.UI.WebControls.TextBox tb_webhead; //网页title
protected System.Web.UI.WebControls.TextBox tb_author; //作者
protected System.Web.UI.WebControls.TextBox tb_title; //页面新闻标题
private string str = "";//声明一个字符串
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.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
//***部分是Web 窗体设计器生成的代码

//自定义一个bool值的函数
private bool WriteFile(string htmlfilename,string strArticle, string strBiaoti,string strContent,string strAuthor)
{//其中news是项目中的一个文件夹,并且要给与适当的权限
string path = HttpContext.Current.Server.MapPath("news/");
Encoding code = Encoding.GetEncoding("gb2312");
// 读取模板文件
string temp= HttpContext.Current.Server.MapPath("news/TextModel.html");
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 读取文件
sr.Close();
}
catch(Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
return false;
}
//替换内容
//这时,模板文件已经读入到名称为str的变量中了
str = str.Replace("$ShowArticle$", strArticle);//模板页中的ShowArticle
str = str.Replace("$Biaoti$", strBiaoti);
str = str.Replace("$Content$", strContent);
str = str.Replace("$Author$", strAuthor); str=str.Replace("$WriterTime$",System.DateTime.Now.ToShortDateString());
// 写文件
ViewState["P"] = path + htmlfilename;
try
{
sw = new StreamWriter(path + htmlfilename, false, code);
sw.Write(str);
sw.Flush();//清除当前编辑器的缓冲区
}
catch(Exception ex)
{ HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
return false;
}
finally
{
sw.Close();
}
return true;
}
private void Button1_Click(object sender, System.EventArgs e)
{//发表内容
string htmlfilename = System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";

if (WriteFile(htmlfilename,tb_webhead.Text,tb_title.Text,tb_content.Text,tb_author.Text))
{//如果发布成功 //Response.Write("<script>window.opener=null;window.close();</script>");
Response.Write("<script>alert('发布成功!');</script>");
}
}
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: