.net生成静态页面并生成分页代码

作者:袖梨 2022-06-25

int num = 0;
      
        string sql = "insert into news(title,content,bigclassname,[user],infotime,hits,upfile) values(@title,@content,@ddl,@name,@time,@num,@filename)";
        oledbparameter[] para = new oledbparameter[] {
        new oledbparameter("@title",txttitle.text),
           
              new oledbparameter("@content",fckeditor1.value),
              new oledbparameter("@ddl",ddllei.selecteditem.value),
              new oledbparameter("@name",txtname.text),
              new oledbparameter("@time",txttime.text),
              new oledbparameter("@num",num),
              new oledbparameter("@filename",filename)
        };
        db.executecommand(sql, para);
        string mbpath = server.mappath("../template/news.html");
        encoding code = encoding.getencoding("gb2312");
        streamreader sr = null;
        streamwriter sw = null;
        string str = null;

        //读取
        try
        {
            sr = new streamreader(mbpath, code);
            str = sr.readtoend();

        }
        catch (exception ex)
        {
            throw ex;
        }
        finally
        {
            sr.close();
        }
        string strmax = "select id from news order by id desc";
        int newsid = convert.toint32(db.readout2(strmax))+1;
        //根据时间自动重命名,扩展名也可以自行修改
        string filename = "news_" + newsid + ".shtml";

        str = str.replace("#title#", txttitle.text);
        str = str.replace("#infotime#", txttime.text);
        str = str.replace("#content#", fckeditor1.value);
        //生成静态文件
        try
        {
            sw = new streamwriter(server.mappath("http://www.cnblogs.com/") + filename, false, code);
            sw.write(str);
            sw.flush();

fenye();

        }
        catch (exception ex)
        {
            throw ex;
        }
        finally
        {
            sw.close();
          
        }
        response.write("");

//分页

public void fenye()

{

 string str = "select * from news order by id desc";
        datatable tb = db.mydataadapter(str);
        datarow dr;


        //最后一页是否整页
        int m = 0;
        //页数
        int pg = 0;
        if (tb.rows.count % 10 == 0)
        {
            pg = tb.rows.count / 10;
        }
        else
        {
            pg = convert.toint32(tb.rows.count / 10) + 1;
            m = 1;
        }

        //分页
        string pglist = "";
        for (int i = 1; i <= pg; i++)
        {
            pglist += " " + i + "";
        }
        pglist = "

共"+pg+"页 首页" + pglist + " 尾页
";


        for (int j = 1; j <= pg; j++)
        {
            //建立一个stringbuilder对象
            stringbuilder sb = new stringbuilder();

            //读取模板
            using (streamreader sr = new streamreader(server.mappath("../template/index.html"), system.text.encoding.default, true))
            {

                //将模板写入streambuilder
                sb.append(sr.readtoend().tostring());
            }

            //删除原有文件
           // file.delete("../....//news/" + j.tostring() + ".html");

            string title = "";
            string ublist = "";
            string infotime = "";
            string id = "";
            if (m == 1 && j == pg)//不是整页
            {
                //for (int i = 1 + 10 * (j - 1); i <= (tb.rows.count - 10 * (pg - 1)); i++)
                for (int i = 1 + 10 * (j - 1); i <= (tb.rows.count); i++)
                {
                    dr = tb.rows[i - 1];
                    title = dr["title"].tostring();
                    id = dr["id"].tostring();
                    infotime = dr["infotime"].tostring();
                    ublist += "

";
                    ublist += "
"+i+"→" + title + " " + infotime + "
";
                  
                }
            }
            else//是整页
            {
                for (int i = 1 + 10 * (j - 1); i <= 10 * j; i++)
                {
                    dr = tb.rows[i - 1];
                    id = dr["id"].tostring();
                    title = dr["title"].tostring().replace(" ", "");
                    infotime = dr["infotime"].tostring().replace(" ", "");
                    ublist += "";
                    ublist += "
" + i + "→" + title + " " + infotime + "
";
                }
            }

            //替换模板中的内容...

            sb.replace("$title$", ublist);
                     //替换分页
            sb.replace("$pglist$", pglist);


            using (streamwriter sw = new streamwriter(server.mappath("http://www.cnblogs.com/news/" + j.tostring() + ".html"), false, system.text.encoding.default, 100))
            {

                //写出.html文件
                sw.writeline(sb);

                sw.flush();

                sw.close();
            }

        }

}

一键生成静态页面

protected void btnhtml_click(object sender, eventargs e)
    {
        string sql = "select * from news order by id desc";
        datatable dt = db.mydataadapter(sql);
        if (dt.rows.count > 0)
        {
            for (int i = 0; i < dt.rows.count; i++)
            {
                string mbpath = server.mappath("../template/news.html");
                encoding code = encoding.getencoding("gb2312");
                streamreader sr = null;
                streamwriter sw = null;
                string str = null;

                //读取
                try
                {
                    sr = new streamreader(mbpath, code);
                    str = sr.readtoend();

                }
                catch (exception ex)
                {
                    throw ex;
                }
                finally
                {
                    sr.close();
                }
              
                //根据时间自动重命名,扩展名也可以自行修改
                string filename = "news_" + dt.rows[i]["id"].tostring() + ".shtml";
              
                str = str.replace("#title#", dt.rows[i]["title"].tostring());
                str = str.replace("#infotime#", dt.rows[i]["infotime"].tostring());
                str = str.replace("#content#", dt.rows[i]["content"].tostring());
                //生成静态文件
                try
                {
                    sw = new streamwriter(server.mappath("http://www.cnblogs.com/" + "news" + "/") + filename, false, code);
                    sw.write(str);
                    sw.flush();
                    this.page.clientscript.registerstartups教程cript(gettype(), "", "");

                }
                catch (exception ex)
                {
                    throw ex;
                }
                finally
                {
                    sw.close();
                  
                }
            }

        }
        else
        {
            this.page.clientscript.registerstartupscript(gettype(), "", "");
        }
    }

相关文章

精彩推荐