ASP.NET生成HTML静态页面实例

作者: unvs 分类: ASP.NET 发布时间: 2011-02-23 20:48 ė12,035 views 6没有评论

1 配置WEB.CONFIG  

<appSettings>    <!--保存静态页路径-->    <add key="htmlPath" value="D:JunvalcreateHtmhtml"/></appSettings>
<appSettings> <!–保存静态页路径–> <add key="htmlPath" value="D:JunvalcreateHtmhtml"/> </appSettings>

2.创建模板页
 

 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html>    <head>        <title>my_title</title>        <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">        <meta name="ProgId" content="VisualStudio.HTML">        <meta name="Originator" content="Microsoft Visual Studio .NET 7.1">    </head>    <body>        生成时间:my_time        <br>        作者: my_name        <br>        <hr>        替换的正文:my_body        <h2>            [ DllImport( "kernel32", EntryPoint="GetVersionEx" )]             DllImportAttribute特性的公共字段如下: 1、CallingConvention 指示向非托管实现传递方法参数时所用的             CallingConvention 值。 CallingConvention.Cdecl : 调用方清理堆栈。它使您能够调用具有 varargs 的函数。             CallingConvention.StdCall : 被调用方清理堆栈。它是从托管代码调用非托管函数的默认约定。 2、CharSet             控制调用函数的名称版本及指示如何向方法封送 String 参数。 此字段被设置为 CharSet 值之一。如果 CharSet 字段设置为             Unicode,则所有字符串参数在传递到非托管实现之前都转换成 Unicode 字符。</h2>    </body></html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>my_title</title> <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1"> <meta name="ProgId" content="VisualStudio.HTML"> <meta name="Originator" content="Microsoft Visual Studio .NET 7.1"> </head> <body> 生成时间:my_time <br> 作者: my_name <br> <hr> 替换的正文:my_body <h2> [ DllImport( "kernel32", EntryPoint="GetVersionEx" )] DllImportAttribute特性的公共字段如下: 1、CallingConvention 指示向非托管实现传递方法参数时所用的 CallingConvention 值。 CallingConvention.Cdecl : 调用方清理堆栈。它使您能够调用具有 varargs 的函数。 CallingConvention.StdCall : 被调用方清理堆栈。它是从托管代码调用非托管函数的默认约定。 2、CharSet 控制调用函数的名称版本及指示如何向方法封送 String 参数。 此字段被设置为 CharSet 值之一。如果 CharSet 字段设置为 Unicode,则所有字符串参数在传递到非托管实现之前都转换成 Unicode 字符。</h2> </body> </html>

3. 写静态页类
 

using System;using System.IO;namespace Junval.createHtm{    /// <summary>    /// ExcetueHtm 的摘要说明。    /// </summary>    public class ExcetueHtm    {        private string sId;   //需要生成静态页的数据ID        private string sTemp; //需要生成静态页的模板名称        public ExcetueHtm()        {            //            // TODO: 在此处添加构造函数逻辑            //        }        /// <summary>        /// 设置ID属性        /// </summary>        public string ID        {            get { return sId; }            set { sId = value; }        }        public string Temp        {            get { return sTemp; }            set { sTemp = value; }        }        /// <summary>        /// 生成静态页面       &
nbsp;/// </summary>        /// <returns></returns>        public bool CreateHtml()        {            //存放HTML路径            string ls_path = System.Configuration.ConfigurationSettings.AppSettings["htmlPath"].ToString();            //选择模板            string ls_temp = ls_path + sTemp;            System.IO.StreamReader Sr = null;            System.IO.StreamWriter Sw = null;            string str = "";            //读模板            try            {                Sr = new StreamReader(ls_temp, System.Text.Encoding.GetEncoding("GB2312"));                str = Sr.ReadToEnd();            }            catch (Exception ex)            {                System.Web.HttpContext.Current.Response.Write(ex.Message.ToString());                System.Web.HttpContext.Current.Response.End();            }            finally            {                Sr.Close();            }            string sFilename = sId.ToString() + ".htm";            //替换模板内容            str = ReplaceStr(str);            //写vhtml            try            {                Sw = new StreamWriter(ls_path + sFilename, false, System.Text.Encoding.GetEncoding("gb2312"));                Sw.Write(str);                Sw.Flush();            }            catch (Exception ex)            {                System.Web.HttpContext.Current.Response.Write(ex.Message.ToString());                System.Web.HttpContext.Current.Response.End();            }            finally            {                Sw.Close();            }            return true;        }        private string ReplaceStr(string ls_str)        {            //根据模板 选择不通的替换函数            return TempNo_1(ls_str).ToString();        }        //NO1模板替换方案        private string TempNo_1(string ls_str)        {            string ls_Tilte = "标题:一号模板替换方案";            string ls_time = DateTime.Now.ToString();            ls_str = ls_str.Replace("my_title", ls_Tilte);            ls_str = ls_str.Replace("my_time", ls_time);            ls_str = ls_str.Replace("my_name", "Junval Shi");            ls_str = ls_str.Replace("my_body", " ......");            return ls_str;        }        /// <summary>        /// 生成静态页面        /// </summary>        /// <param name="pid">需要生成静态页的数据ID</param>        /// <param name="ptemp">需要生成静态页的模板名称</param>        /// <returns></returns>        public bool CreateHtml(string pid, string ptemp)        {            return true;        }    }}

 

 
<%@ Page language="c#" Codebehi
nd="Main.aspx.cs" AutoEventWireup="false" Inherits="Junval.createHtm.Main" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML>    <HEAD>        <title>Main</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="TextBox1" style="Z-INDEX: 101; LEFT: 168px; POSITION: absolute; TOP: 88px" runat="server"></asp:TextBox>            <asp:Button id="CreateHtml" style="Z-INDEX: 102; LEFT: 368px; POSITION: absolute; TOP: 88px"                runat="server" Text="CreateHtml"></asp:Button>        </form>    </body></HTML>

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;namespace Junval.createHtm{    /// <summary>    /// Main 的摘要说明。    /// </summary>    public class Main : System.Web.UI.Page    {        protected System.Web.UI.WebControls.TextBox TextBox1;        protected System.Web.UI.WebControls.Button CreateHtml;        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.CreateHtml.Click += new System.EventHandler(this.CreateHtml_Click);            this.Load += new System.EventHandler(this.Page_Load);        }        #endregion        private void CreateHtml_Click(object sender, System.EventArgs e)        {            string ls_id = TextBox1.Text.Trim();            ExcetueHtm CH = new ExcetueHtm();            CH.ID = ls_id;            CH.Temp = "HTMLPage1.html";            if (CH.CreateHtml())            {                Response.Write("<script>window.open('html/" + ls_id + ".htm','','');</script>");            }     &nb
sp;      else            {                Response.Write("ErrEs");            }        }    }}

 

本博文章基本上属于原创或收集整理,都是心血结晶。
欢迎转载分享,转载请注明出处,谢谢!
本文地址:ASP.NET生成HTML静态页面实例

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Ɣ回顶部