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.cnblogs.com/") + fileName, false, code);
            sw.Write(str);
            sw.Flush();

fenye();

        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            sw.Close();
          
        }
        Response.Write("<script language=javascript>alert('添加成功!');location.href='NewsManage.aspx';</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.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.RegisterStartupScript(GetType(), "", "<script>alert('生成成功!')</script>");

                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    sw.Close();
                  
                }
            }

        }
        else
        {
            this.Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('暂无数据')</script>");
        }
    }

posted @ 2011-04-04 15:51  放哨De老鼠  阅读(486)  评论(6编辑  收藏  举报