生成静态页面——C#+asp.net————重点在于Replace

主要包含三个页面文件TempHtml.html(模板文件),default.aspx(上传新闻内容页面),deault.aspx.cs(后台实现代码):

新闻模板文件(TempHtml.html):
 <html>
       <head>
              <title>$htmlkey[0]</title>
              <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
       </head>
       <body >
              <table $htmlkey[1] 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: $htmlkey[2];font-size: $htmlkey[3]"><marquee>$htmlkey[4]</marquee></span>
     </td>
                 </tr>
              </table>
       </body>
</html>

上传新闻内容页面:default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="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>Untitled Page</title>
</head>
<body>
   <form >
      <asp:Button
       runat="server" Text="创建HTML文件" OnClick="btnCreate_Click"></asp:Button>
      <asp:TextBox
       runat="server" TextMode="MultiLine" Height="402px" Width="352px"></asp:TextBox>
      <asp:HyperLink
       runat="server" Target="_blank">创建的HTML文件</asp:HyperLink>
      <asp:TextBox
       Width="352px"></asp:TextBox>
      <asp:Label >页面标题</asp:Label>
      <asp:Label >页面内容</asp:Label>
  </form>
</body>
</html>

实现代码:default.aspx.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;//StringBuilder 类命名空间
using System.IO;//读取写入命名空间
public partial class Dhtml : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        string[] newContent = new string[5];//定义数组用来保存temphtml.html中的标记数目
        StringBuilder strhtml = new StringBuilder();//实例化一个strhtml
        try
        {
            //创建StreamReader对象 news为TempHtml.html所在的文件夹
            using (StreamReader sr = new StreamReader(Server.MapPath("news") + "\\TempHtml.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("news") + "\\" + 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 = "news/" + DateTime.Now.ToString("yyyymmddhhmmss") + ".html";
        }
        catch (Exception err)
        {
            Response.Write(err.ToString());
        }
    }

相关文章:

  • 2021-12-14
  • 2022-02-14
  • 2021-12-30
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
  • 2022-12-23
猜你喜欢
  • 2021-07-21
  • 2021-05-18
  • 2021-09-17
相关资源
相似解决方案