Asp.net生成靜態頁面並直接生成分頁的代碼實例

    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.3ppt.com/") + filename, false, code);
                sw.write(str);
                sw.flush();

    fenye();

            }
            catch (exception ex)
            {
                throw ex;
            }
            finally
            {
                sw.close();
             
            }
            response.write("<script language=網頁特效>alert('添加成功!');location.href='newsmanage.asp教程x';</script>");

    //分頁

    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 += " <a href="" + i + ".html">" + i + "</a>";
            }
            pglist = "<div >共"+pg+"頁&nbsp;<a href="1.html">首頁</a>" + pglist + "<a href="" + pg + ".html">&nbsp;尾頁</a></div> ";


            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 += "<table border="0" cellpadding="0" cellspacing="0" style="width:610px; line-height:10px" >";
                        ublist += " <tr ><td align="left"> "+i+"<a href="news/news_" + id + ".shtml">→" + title + "</a> </td><td align="right" >" + infotime + "</td></tr></table>";
                     
                    }
                }
                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 += "<table border="0" cellpadding="0" cellspacing="0" style="width:610px; line-height:25px" >";
                        ublist += " <tr ><td align="left"> " + i + "<a href="news/news_" + id + ".shtml">→" + title + "</a> </td><td align="right" >" + infotime + "</td></tr></table> ";
                    }
                }

                //替換模板中的內容...

                sb.replace("$title$", ublist);
                         //替換分頁
                sb.replace("$pglist$", pglist);


                using (streamwriter sw = new streamwriter(server.mappath("http://www.3ppt.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.3ppt.com/" + "news" + "/") + filename, false, code);
                        sw.write(str);
                        sw.flush();
                        this.page.clientscript.registerstartups教程cript(gettype(), "", "<script>alert('生成成功!')</script>");

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

            }
            else
            {
                this.page.clientscript.registerstartupscript(gettype(), "", "<script>alert('暫無數據')</script>");
            }
        }html

相關文章
相關標籤/搜索