Sampson-Li
Sampson.Li学习经验总结博客 学历代表过去,能力代表现在.学习力代表未来!

一个简单的内容分页.实现就是在内容插入的时候,在需要分页的地方,放入一个分页的表示符,然后分隔文章显示. 

 public class Pager
    {

  //content是内容的str字符串,page是第几页
        public static string ContentPage(string content,int page)
        {
            string[] array_content = content.Split(new string[] { "{$:page}" }, StringSplitOptions.None);
            int length = array_content.Length;
            if (length <= 1)
            {
                return content;
            }

       //返回内容以及分页符
            return array_content[page - 1] + MakePage(length,page);
        }

       //创建分页字符1 2 3
        public static string MakePage(int length,int page)
        {
            string url;
            int ic = HttpContext.Current.Request.RawUrl.IndexOf("&page");
            if (ic > 0)
            {
                url = HttpContext.Current.Request.RawUrl.Remove(ic);
            }
            else
            {
                url = HttpContext.Current.Request.RawUrl;
            }
            StringBuilder builder = new StringBuilder();
            builder.Append("<table id=\"pagelist\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\" width=\"100%\">");
            builder.Append("<tr><td align=\"center\"><ul><li><a href=\"" + url + "\">首页</a></li><li><a href=\"" + url + "&page=" + (page - 1).ToString() + "\">上一页</a></li>");
            for (int i = 0; i < length; i++)
            {
                if (page - 1 == i)
                {
                    builder.Append("<li class=\"current\">" + (i + 1).ToString() + "</li>");
                }
                else
                {
                    builder.Append("<li><a href=\"" + url + "&page=" + (i + 1).ToString() + "\">" + (i + 1).ToString() + "</a></li>");
                }
            }
            if (page < length)
            {
                builder.Append("<li><a href=\"" + url + "&page=" + (page + 1).ToString() + "\">下一页</a></li>");
            }
            else
            {
                builder.Append("<li><a href=\"" + url + "&page=" + page.ToString() + "\">下一页</a></li>");
            }
            builder.Append("<li><a href=\"" + url + "&page=" + length.ToString() + "\">尾页</a></li></ul></td></tr></table>");
            return builder.ToString();
        }
    }

posted on 2011-11-02 09:24  Sampson  阅读(712)  评论(2编辑  收藏  举报