现在有这么一个问题 美工写的HTML页面 程序不管它是什么页面 就是程序运行的时候用正则将HTML页面替换成ASP.NET页面(主要是和数据库进行交互Asp.NET页面是空的 )

解决方案 »

  1.   

    <%@ Page language="c#" Codebehind="CreatHtml.aspx.cs" AutoEventWireup="false" Inherits="CommonFunction.CreatHtml" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>CreatHtml</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:Button id="btnCreate" style="Z-INDEX: 101; LEFT: 576px; POSITION: absolute; TOP: 48px"
    runat="server" Text="创建HTML文件"></asp:Button>
    <asp:TextBox id="txtContent" style="Z-INDEX: 102; LEFT: 208px; POSITION: absolute; TOP: 80px"
    runat="server" TextMode="MultiLine" Height="402px" Width="352px"></asp:TextBox>
    <asp:HyperLink id="hyCreateFile" style="Z-INDEX: 103; LEFT: 584px; POSITION: absolute; TOP: 96px"
    runat="server" Target="_blank">创建的HTML文件</asp:HyperLink>
    <asp:TextBox id="txtTitle" style="Z-INDEX: 104; LEFT: 208px; POSITION: absolute; TOP: 48px" runat="server"
    Width="352px"></asp:TextBox>
    <asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 128px; POSITION: absolute; TOP: 48px" runat="server">页面标题</asp:Label>
    <asp:Label id="Label2" style="Z-INDEX: 106; LEFT: 128px; POSITION: absolute; TOP: 80px" runat="server">页面内容</asp:Label>
    </form>
    </body>
    </HTML> private void btnCreate_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] = txtTitle.Text;//标题
    newContent[1] = "BackColor='#cccfff'";//背景色
    newContent[2] = "#ff0000";//字体颜色
    newContent[3] = "100px";//字体大小
    newContent[4] = txtContent.Text;//主要内容 //根据上面新的内容生成html文件
    try
    {
    //指定要生成的HTML文件
    string fname = Server.MapPath("createHTML") +"\\" + DateTime.Now.ToString("yyyymmddhhmmss") + ".html";
    //替换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();
    }
    //设置超级链接的属性
    hyCreateFile.Text = DateTime.Now.ToString("yyyymmddhhmmss")+".html";
    hyCreateFile.NavigateUrl = "createHTML/"+DateTime.Now.ToString("yyyymmddhhmmss")+".html";
    }
    catch(Exception err)

    Response.Write (err.ToString());
    }
    }