当前位置: 代码迷 >> ASP.NET >> asp.net 动态生成htm页解决办法
  详细解决方案

asp.net 动态生成htm页解决办法

热度:9012   发布时间:2013-02-25 00:00:00.0
asp.net 动态生成htm页
苦苦的寻觅,没有结果。最后的希望寄托于此!
请高手们给个例子,帮讲解下,怎么编写生成htm页面的asp.net程序?
怎么样能让他们整体组合起来,不是单一的生成一页。
比如说postnew.aspx?room=5202   变成     ****.htm

------解决方案--------------------------------------------------------
写一个:
先新建一个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 "))
{
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 ")+ " ";
this.HyperLink1.NavigateUrl= "createHTML/ "+DateTime.Now.ToString( "yyyymmddhhmmss ")+ ".liping19851014 ";//这里后缀可以随便改
  相关解决方案