服务导航
企业概况     李墨菲博客
百度推广、谷歌推广、SEO优化
域名注册、服务器 VPS租用
网站服务流程    业务渠道代理
网站建设价格表
搜索引擎竞价排名服务
800元企业网站制作特价套餐
搜索引擎免费登录口
咨询QQ:2196677
售后QQ:82090889
为什么要选择八桂网讯?
8年品牌网站建设经验积累,网站成功客户超2200家!
在为您提供服务的任何一个环节,我们都拥有自己的核心团队,保障售后服务的可控性。
由程序编程师、平面美工设计师形成的专业后盾组合,从asp、asp.net、php到java的知识结构互补,使得八桂网讯能够满足客户的功能需求和二次开发整合。
全面的成熟团队为能为您提供一流的平面设计和网站管理后台。
当前位置:网站首页 新闻资讯ASP.NET生成静态网页的方法
ASP.NET生成静态网页的方法
编辑:八桂网讯    时间:2009/7/9    浏览:1894
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"
八桂网讯 爱伦文化 深圳外语培训 招商加盟 迪优目录 广西旅游365 八桂大地 建站导航网 南宁创意雕塑 双成纸管
八桂网讯-高端网站建设专家
Copyright © 2004- 八桂网讯网络技术有限公司 Baguidadi Inc. All Rights Reserved.