欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Asp.Net生成静态页面的实现方法

程序员文章站 2024-02-27 18:56:33
复制代码 代码如下:using system; using system.data; using system.configuration; using system.co...

复制代码 代码如下:

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 mysqlserver;//数据库操作类
using system.io;
using system.text;
namespace newsadd
{
    public partial class admin_adminpanel_newsadd : system.web.ui.page
    {
        protected void page_load(object sender, eventargs e)
        {

        }
        protected void button1_click(object sender, eventargs e)
        {
            string strtitle=request.form["title"].tostring();
            string strcontent=request.form["content"].tostring();
            sqlserverdatabase db = new sqlserverdatabase();
            bool success = db.insert("insert into innews(title,content)values('" + strtitle + "','" + strcontent + "')", null);
            //if (success)
               // message.text = "添加成功!";
            /**////////////////////////////创建当前日期的文件夹开始
            string dir = server.mappath("../../"+"newsfiles/"+datetime.now.tostring("yymmdd"));
            if (!directory.exists(dir))
            {
                directory.createdirectory(dir);
            }
            /**////////////////////////////创建当前日期的文件夹结束

            string[] newcontent = new string[5];//定义和html标记数目一致的数组
            stringbuilder strhtml = new stringbuilder();
            try
            {
                //创建streamreader对象
                using (streamreader sr = new streamreader(server.mappath("../../" + "newsfiles/") + "/template.html"))
                {
                    string oneline;
                    //读取指定的html文件模板
                    while ((oneline = sr.readline()) != null)
                    {
                        strhtml.append(oneline);
                    }
                    sr.close();
                }
            }
            catch (exception err)
            {
                //输出异常信息
                response.write(err.tostring());
            }
            //为标记数组赋值
            newcontent[0] = strtitle;//标题
            newcontent[1] = "backcolor='#cccfff'";//背景色
            newcontent[2] = "#ff0000";//字体颜色
            newcontent[3] = "100px";//字体大小
            newcontent[4] = strcontent;//主要内容

            //根据上面新的内容生成html文件
            try
            {
                //指定要生成的html文件
                string fname = server.mappath("../../" + "newsfiles/" + datetime.now.tostring("yymmdd")) + "/" + 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();
                }
            }
            catch (exception err)
            {
                response.write(err.tostring());
            }
        }
}
}


template.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" mce_style="border:1px solid #000000">
                 <tr>
                    <td width="100%" valign="middle" align="left">
                        <span style="color: $htmlkey[2];font-size: $htmlkey[3]" mce_style="color: $htmlkey[2];font-size: $htmlkey[3]">$htmlkey[4]</span>
                    </td>
                 </tr>
              </table>
       </body>
</html>