asp.net生成静态页面

来源:互联网 发布:java培训机构0首付 编辑:程序博客网 时间:2024/04/30 10:03
cs:

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;

public partial class JingTaiPage_Default : System.Web.UI.Page
{
      
protected void Page_Load(object sender, EventArgs e)
      
{

      }

      
protected void Button1_Click(object sender, EventArgs e)
      
{
          
string[] format = new string[4];
          StringBuilder htmltext 
= new StringBuilder();

          
try
          
{
              
using (StreamReader sr = new StreamReader(@Server.MapPath("miao.html")))
              
{
                  String line;
                  
while ((line = sr.ReadLine()) != null)
                  
{
                      htmltext.Append(line);
                  }

                  sr.Close();
                  sr.Dispose();
              }

          }

          
catch(Exception exp)
          
{
              Page.RegisterStartupScript(
"key","<scipt>alert('"+exp.ToString()+"');</scipt>");
              
//Response.Write(exp.ToString());
              
//Response.Write("<script>alert('读取文件错误');</script>");
              
//Response.End();
          }


          
//---------------------给标记数组赋值------------ 
          format[0= "background="bg.jpg"";//背景图片 
          format[1= "#990099";//字体颜色 
          format[2= "150px";//字体大小 
          format[3= TextBox1.Text.ToString();//文字说明 

          
for (int i = 0; i < 4; i++)
          
{
              htmltext.Replace(
"$htmlformat[" + i + "]",format[i]);
          }


          
try
          
{
              
string strTime = System.DateTime.Now.ToShortDateString();
       
    
              
using (StreamWriter sw = new StreamWriter(@Server.MapPath("" + strTime + ".html"), false, System.Text.Encoding.GetEncoding("GB2312")))
              
{
                  sw.WriteLine(htmltext);
                  sw.Flush();
                  sw.Close();
                  sw.Dispose();
                  Page.RegisterStartupScript(
"key""<script>alert('生成静态页面成功');</script>");
              }

          }

          
catch(Exception exp)
          
{
              Response.Write(exp.ToString());
          }


      }

}

aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="JingTaiPage_Default" %>

<!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:TextBox ID="TextBox1" runat="server"></asp:TextBox>
          
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="生成静态页面" /></div>
      
</form>
</body>
</html>


模版页:miao.html

<html> 

<head> 

<title></title> 

<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 

</head> 

<body > 

<table $htmlformat[0] height="100%" border="0" width="100%" cellpadding="10" cellspacing="0" bgcolor="#eeeeee" style="border:1px solid #000000"> 

<tr> 

<td width="100%" valign="middle" align="left"> 

<span style="color: $htmlformat[1];font-size: $htmlformat[2]">$htmlformat[3]</span> 

</td> 

</tr> 

</table> 

</body> 

</html>
 
原创粉丝点击