利用模版asp.net生成静态页面
2009-06-11 12:05
260 查看
模版页(我把里面放了一个table,页面有2个标记 title1和content)-----------------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>title1</title>
</head>
<body>
<table style="width: 522px">
<tr>
<td>
</td>
<td>
</td>
<td style="width: 170px">
</td>
</tr>
<tr>
<td style="height: 21px">
</td>
<td style="height: 21px">
</td>
<td style="width: 170px; height: 21px">
</td>
</tr>
<tr>
<td style="height: 130px">内容:content
</td>
<td style="height: 130px">
</td>
<td style="width: 170px; height: 130px">
</td>
</tr>
</table>
</body>
</html>
前台页面asp.aspx(有textbook1和textbook2分别用于设置生静态页面的title和content)---------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="标题" Width="101px"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Width="171px"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text="内容" Width="99px"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Height="175px" TextMode="MultiLine"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="生成静态页面" /></div>
</form>
</body>
</html>
后台代码-(主要是button的事件代码)-------------------------------------------------------------------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Net;
public partial class Default2 : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
string mypath = Server.MapPath("template.htm");
Encoding code = Encoding.GetEncoding("gb2312");
StreamReader sr = null;
StreamWriter sw = null;
string str = "";
//把模版页读入到数据流
try
{
sr = new StreamReader(mypath, code);
str = sr.ReadToEnd();
}
catch (Exception)
{
Response.Write("错误01");
}
finally
{
sr.Close();
}
// 使用系统时间自动生成文件名
string fileName = System.DateTime.Now.ToString("yyyyMMddhhmmss") + ".htm";
//更改页面的title 和 内容
str = str.Replace("title1", TextBox1.Text);
str = str.Replace("content", TextBox2.Text);
//写入到指定htm文件
try
{
sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception)
{
Response.Write("错误02");
}
finally
{
sw.Close();
Response.Write("<a href = htm/" + fileName + " target=_black>" + fileName + "");
}
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>title1</title>
</head>
<body>
<table style="width: 522px">
<tr>
<td>
</td>
<td>
</td>
<td style="width: 170px">
</td>
</tr>
<tr>
<td style="height: 21px">
</td>
<td style="height: 21px">
</td>
<td style="width: 170px; height: 21px">
</td>
</tr>
<tr>
<td style="height: 130px">内容:content
</td>
<td style="height: 130px">
</td>
<td style="width: 170px; height: 130px">
</td>
</tr>
</table>
</body>
</html>
前台页面asp.aspx(有textbook1和textbook2分别用于设置生静态页面的title和content)---------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="标题" Width="101px"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Width="171px"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Text="内容" Width="99px"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Height="175px" TextMode="MultiLine"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="生成静态页面" /></div>
</form>
</body>
</html>
后台代码-(主要是button的事件代码)-------------------------------------------------------------------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Net;
public partial class Default2 : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
string mypath = Server.MapPath("template.htm");
Encoding code = Encoding.GetEncoding("gb2312");
StreamReader sr = null;
StreamWriter sw = null;
string str = "";
//把模版页读入到数据流
try
{
sr = new StreamReader(mypath, code);
str = sr.ReadToEnd();
}
catch (Exception)
{
Response.Write("错误01");
}
finally
{
sr.Close();
}
// 使用系统时间自动生成文件名
string fileName = System.DateTime.Now.ToString("yyyyMMddhhmmss") + ".htm";
//更改页面的title 和 内容
str = str.Replace("title1", TextBox1.Text);
str = str.Replace("content", TextBox2.Text);
//写入到指定htm文件
try
{
sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception)
{
Response.Write("错误02");
}
finally
{
sw.Close();
Response.Write("<a href = htm/" + fileName + " target=_black>" + fileName + "");
}
}
}
相关文章推荐
- ASP.NET技巧:利用模版生成html页
- ASP.NET技巧:利用模版生成html页
- 利用UrlRewrite,asp.net动态生成静态页面
- asp.net利用OWC生成饼状图、柱状图
- ASP.NET生成静态页面和分页代码 http://blog.csdn.net/zdyguilong/archive/2007/05/24/1624491.aspx
- Asp.net 静态页面生成(3)----动态页生成之 生成爬虫
- ASP.NET生成静态页面的简单实现
- 使用ASP.NET模板生成HTML静态页面
- Asp.net 生成静态页面
- asp.net 不用模板生成静态页面
- asp.net利用itextsharp生成pdf文档
- ASP.NET生成静态页面
- [转]Asp.NET生成静态页面并分页
- ASP.NET中利用OWC组件生成EXCEL表(转)
- Asp.NET生成静态页面并分页
- 利用ASP.NET一般处理程序动态生成Web图像(转)
- ASP.NET生成静态页面的方法
- ASP.NET生成静态页面实现方法
- ASP.NET生成静态页面
- Asp.NET生成静态页面并分页