用模板页面 生成静态页面 实现文章分页

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Text;

public partial class css_Default : System.Web.UI.Page
{

    protected SqlDataReader sdr;
    protected string title;
    protected string time;
    protected string source;
    protected string author;
    protected string guide;
    protected string content;
    protected string links;//链接字符串

    protected int Page_Size=800;
   

    protected void Page_Load(object sender, EventArgs e)
    {
        sdr = life.article_all_bytid(37);

        while (sdr.Read())
        {
            title = sdr["TTITLE"].ToString();
            time = sdr["TTIME"].ToString();
            source = sdr["TSOURCE"].ToString();
            author = sdr["TAUTHOR"].ToString();
            guide = sdr["TGUIDE"].ToString();
            content = sdr["TCONTENT"].ToString();

        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {

        int Total_Page;//总页数
        string[] subed;//截取内容


        string oldpath = Server.MapPath("~/template/article.html");
        string oldpath2 = Server.MapPath("~/template/article2.html");
        string newpath = Server.MapPath("~/template/articlenew.html");
       

      

        if (content.Length < Page_Size)
        {
             links = "<a href=\"articlenew0.html\" disabled=\"false\"> ["+1+"] </a>";
             CreateNew(Server.MapPath("~/template/articlenew" + 1 + ".html"), ReadContet(oldpath,links));
           
        }
        else
        {
            Total_Page = Convert.ToInt32(content.Length) / Page_Size + 1;
            subed = new string[Total_Page];

            for (int i = 0; i < Total_Page; i++)
            {
                links += "<a href=\"articlenew" + (i + 1) + ".html\"> [" + (i + 1) + "] </a>";
            }

            for (int i = 0; i < Total_Page; i++)
            {
                if (i == 0)
                {
                    subed[0] = content.Substring(0, Page_Size);
                    CreateNew(Server.MapPath("~/template/articlenew" + 1 + ".html"), ReadContet(oldpath, subed[0], links));
                }


                else if (i > 0 && i < Total_Page-1)
                {
                    subed[i] = content.Substring(Page_Size * i, Page_Size);
                    CreateNew(Server.MapPath("~/template/articlenew" + (i + 1).ToString() + ".html"), ReadContet(oldpath2, subed[i], links));
                }
                else if (i == Total_Page-1)
                {
                    subed[i] = content.Substring(Page_Size * i, content.Length-Page_Size*i);
                    CreateNew(Server.MapPath("~/template/articlenew" + (i + 1).ToString() + ".html"), ReadContet(oldpath2, subed[i], links));
                }


            }

            Response.Write(content.Length);

       }  
      
    }


    protected string ReadContet(string paths, string scontent, string link)
    {
        StreamReader sr = new StreamReader(paths,Encoding.UTF8);

        string str = sr.ReadToEnd();
        str = str.Replace("{title}", title);
        str = str.Replace("{atitle}", title);
        str = str.Replace("{time}", time);
        str = str.Replace("{source}", source);
        str = str.Replace("{author}", author);
        str = str.Replace("{guide}", guide);
        str = str.Replace("{content}", scontent);
        str = str.Replace("{link}", link);

        return str;

    }

    protected string ReadContet(string paths,string link)
    {
        StreamReader sr = new StreamReader(paths, Encoding.UTF8);

        string str = sr.ReadToEnd();
        str = str.Replace("{title}", title);
        str = str.Replace("{atitle}", title);
        str = str.Replace("{time}", time);
        str = str.Replace("{source}", source);
        str = str.Replace("{author}", author);
        str = str.Replace("{guide}", guide);
        str = str.Replace("{content}", content);
        str = str.Replace("{link}", link);
        sr.Close();

        return str;

    }


    protected void CreateNew(string paths, string content)
    {
        StreamWriter sw = new StreamWriter(paths,false, Encoding.UTF8);
        sw.Write(content);
        sw.Flush();
        sw.Close();
    }


}

posted @ 2012-04-21 11:50  sidihu  阅读(307)  评论(0编辑  收藏  举报