ASP.NET生成静态网页的方法 |
编辑:八桂网讯 时间:2009/7/9 浏览:1994 次 |
|
WebHover.aspx
〈%@ Page language="c#" Codebehind="WebHover.aspx.cs" AutoEventWireup="false" Inherits="asp.net.staticPage.WebHover" %〉
〈!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 〉
〈HTML〉
〈HEAD〉
〈title〉WebHover〈/title〉
〈meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"〉
〈meta name="CODE_LANGUAGE" Content="C#"〉
〈meta name="vs_defaultClientScript" content="JavaScript"〉
〈meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"〉
〈/HEAD〉
〈body MS_POSITIONING="GridLayout"〉
〈form id="Form1" method="post" runat="server"〉
〈asp:TextBox id="Title" style="Z-INDEX: 100; LEFT: 192px; POSITION: absolute; TOP: 88px" runat="server"〉〈/asp:TextBox〉
〈asp:Label id="Label3" style="Z-INDEX: 107; LEFT: 120px; POSITION: absolute; TOP: 360px" runat="server"
Width="51px"〉Author〈/asp:Label〉
〈asp:Label id="Label2" style="Z-INDEX: 106; LEFT: 112px; POSITION: absolute; TOP: 168px" runat="server"
Width="57px"〉Content〈/asp:Label〉
〈asp:TextBox id="Content" style="Z-INDEX: 101; LEFT: 192px; POSITION: absolute; TOP: 144px" runat="server"
TextMode="MultiLine" Height="176px" Width="240px"〉〈/asp:TextBox〉
〈asp:TextBox id="Author" style="Z-INDEX: 102; LEFT: 192px; POSITION: absolute; TOP: 360px" runat="server"〉〈/asp:TextBox〉
〈asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 288px; POSITION: absolute; TOP: 424px" runat="server"
Text="Button"〉〈/asp:Button〉
〈asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 120px; POSITION: absolute; TOP: 88px" runat="server"
Width="41px"〉Title〈/asp:Label〉
〈/form〉
〈/body〉
〈/HTML〉
Author Content Title
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.Text;
using System.IO;
namespace asp.net.staticPage
{
/// 〈summary〉
/// WebHover 的摘要说明。
/// 〈/summary〉
public class WebHover : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox Title;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox Author;
protected System.Web.UI.WebControls.TextBox Content;
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
private void Button1_Click(object sender, System.EventArgs e)
{
if(WriteFile(this.Title.Text.ToString(),this.Content.Text.ToString(),this.Author.Text.ToString()))
{
Response.Write("添加成功");
}
else
{
Response.Write("生成HTML出错!");
}
}
public static bool WriteFile(string strText,string strContent,string strAuthor)
{
string path = HttpContext.Current.Server.MapPath("/asp.net/news/");
Encoding code = Encoding.GetEncoding("gb2312");
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("/asp.net/news/text.html");
StreamReader sr=null;
StreamWriter sw=null;
string str="";
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 读取文件
}
catch(Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
str =str.Replace("ShowArticle",strText); //模板页中的ShowArticle
str = str.Replace("biaoti",strText);
str = str.Replace("content",strContent);
str = str.Replace("author",strAuthor);
// 写文件
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();
}
finally
{
sw.Close();
}
return true;
}
}
}
text.html
〈!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"〉
〈html〉
〈head〉
〈title〉ShowArticle〈/title〉
〈meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1"〉
〈meta name="ProgId" content="VisualStudio.HTML" |