ASP.NET 產生HTML靜態頁面執行個體

來源:互聯網
上載者:User

<轉自:http://www.dwww.cn/News/2007-9/2007910179235562.shtml>

 

1 配置WEB.CONFIG

複製XML代碼儲存代碼<appSettings>
<!--儲存靜態頁路徑-->
<add key="htmlPath" value="D:JunvalcreateHtmhtml"/>
</appSettings><appSettings>
<!--儲存靜態頁路徑-->
<add key="htmlPath" value="D:JunvalcreateHtmhtml"/>
</appSettings>

2.建立模板頁
閱讀代碼編輯代碼運行效果複製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 : 被呼叫者清理堆棧。它是從Managed 程式碼調用非託管函數的預設約定。 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 : 被呼叫者清理堆棧。它是從Managed 程式碼調用非託管函數的預設約定。 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()
{
//
// TOD 在此處添加建構函式邏輯
//
}

/// <summary>
/// 設定ID屬性
/// </summary>
public string ID
{
get { return sId; }
set { sId = value; }
}

public string Temp
{
get { return sTemp; }
set { sTemp = value; }
}

/// <summary>
/// 產生靜態頁面
/// </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#" Codebehind="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><%@ Page language="c#" Codebehind="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 Form設計器產生的程式碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。
//
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>");
}
else
{
Response.Write("ErrEs");
}
}
}
}

來源:CSDN

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.