利用模版asp.net生成静态页面

来源:互联网 发布:咸阳广电网络客服热线 编辑:程序博客网 时间:2024/05/22 07:59

模版页(我把里面放了一个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>

       

        &nbsp;<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 + "");
        }
    }

}