写一个:
先新建一个CreatHtml.aspx页 用来创建静态页
主要代码如下:
页面代码:
<form id="Form1" method="post" runat="server">
   <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 200px; POSITION: absolute; TOP: 40px" runat="server"
    Width="240px"></asp:TextBox>
   <asp:TextBox id="TextBox2" style="Z-INDEX: 102; LEFT: 200px; POSITION: absolute; TOP: 88px" runat="server"
    TextMode="MultiLine" Width="456px" Height="280px"></asp:TextBox>
   <asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 456px; POSITION: absolute; TOP: 40px" runat="server"
    Text="创建"></asp:Button>
   <asp:HyperLink id="HyperLink1" style="Z-INDEX: 104; LEFT: 656px; POSITION: absolute; TOP: 88px"
    runat="server">HTML页</asp:HyperLink>
   <DIV style="DISPLAY: inline; Z-INDEX: 105; LEFT: 112px; WIDTH: 78px; POSITION: absolute; TOP: 88px; HEIGHT: 36px"
    ms_positioning="FlowLayout">页面内容:</DIV>
   <DIV style="DISPLAY: inline; Z-INDEX: 106; LEFT: 112px; WIDTH: 88px; POSITION: absolute; TOP: 40px; HEIGHT: 36px"
    ms_positioning="FlowLayout">页面标题:</DIV>
  </form>

后台程序:
实现引用这两个类:
using System.Text;
using System.IO;

在创建按钮事件中:
private void Button1_Click(object sender, System.EventArgs e)
  {
  string[] newContent=new string[5];//定义和HTML标记数目一致得数组
   StringBuilder strhtml=new StringBuilder();
   try
   {
   //创建StreamReader对象
    using(StreamReader sr=new StreamReader(Server.MapPath("createHTML")+"\\template.html"))
    {
    String oneline;
     //读取指定的HTML文件模板
     while((oneline=sr.ReadLine())!=null)
     {
     strhtml.Append(oneline);
     }
     //sr.Close;
    }
   }
   catch(Exception err)
   {
   //输出异常
    Response.Write(err.ToString());
   }
   //为标记数组赋值
   newContent[0]=this.TextBox1.Text;//标题
   newContent[1]="BackColor='#cccfff'";//背景色
   newContent[2]="#ff0000";//字体颜色
   newContent[3]="100px";//字体大小
   newContent[4]=this.TextBox2.Text;//主要内容
   //根据上面新得内容生成html文件
   try
   {
    //指定要生成的html文件
    string fname=Server.MapPath("createHTML")+"\\"+DateTime.Now.ToString("yyyymmddhhmmss")+".liping19851014";//这里后缀可以随便改
    //替换html模板文件中里的标记为新的内容
    for(int i=0;i<5;i++)
    {
    strhtml.Replace("$htmlkey["+i+"]",newContent[i]);
    }
    //创建文件信息对象
    FileInfo finfo=new FileInfo(fname);
    //以打开或者写入的形式创建文件流
    using(FileStream fs=finfo.OpenWrite())
    {
    //根据上面创建的文件流创建写数据流
     StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.GetEncoding("GB2312"));
     //把新的内容写到创建的html页面中
     sw.WriteLine(strhtml);
     sw.Flush();
     sw.Close();
    }
    //设置超级连接的属性
    this.HyperLink1.Text=DateTime.Now.ToString("yyyymmddhhmmss")+".html";
    this.HyperLink1.NavigateUrl="createHTML/"+DateTime.Now.ToString("yyyymmddhhmmss")+".liping19851014";//这里后缀可以随便改
   }
   catch(Exception err)
   {
   Response.Write(err.ToString());
   }
  }
最后是模板页:
template.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>$htmlkey[0]</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>
<table $htmlkey[2] height="100%" border="0" width="100%" cellpadding="10" cellspacing="0" bgcolor=#3366ff>
<tr>
<td>
<span style="color:$htmlkey[2];font-size:$htmlkey[3]"><marquee>$htmlkey[4]</marquee></span>
</td>
</tr>
</table>


</body>
</html>

基本这样就是一个简单实现创建静态页的过程

posted on 2007-05-16 20:50  小角色  阅读(228)  评论(0)    收藏  举报