Asp.net 文章分页

代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

/// <summary>
/// LoafPage 的摘要说明
/// </summary>
public class LoafPage
{

    
public string param = "p";               //要传的参数 (默认)
    public string specialString = "|||";     //用与分页的特殊字符(默认)
    
    
/// <summary>
    
/// 默认构造函数 传参 "p" 特殊字符 "|||"
    
/// </summary>
    public LoafPage()
    {
        
///默认构造函数
    }
    
/// <summary>
    
/// para 是需要传的参数 specialStr 分页需要的特殊字符
    
/// </summary>
    
/// <param name="para"></param>
    
/// <param name="specialStr"></param>
    public LoafPage(string para,string specialStr)
    {
        
this.Param = para;
        
this.SpecialString = specialStr;
    }
    
#region //属性外漏 
    
public string Param
    {
        
get { return param; }
        
set {param= value ; }
    }
    
public string SpecialString
    {
        
get { return specialString; }
        
set {  specialString =value; }
    }
    
#endregion

    
#region  //分页方法
    
/// <summary>
    
/// content 文章全部内容, lit_pageInfo 输出的 分页HTML
    
/// </summary>
    
/// <param name="content"></param>
    
/// <param name="lit_pageInfo"></param>
    
/// <returns></returns>
    public string makePage(string content, Literal lit_pageInfo)
    {
        
string[] conArrays;              //存放所有页的文章
        int pageSize;                   //总页数
        string pageInfo = string.Empty;// 输出的分页html
        int pageid = 1;              //当前页数的ID 默认为1
        if (HttpContext.Current.Request.QueryString[this.Param] != null)
        {
            
try
            {
                pageid 
= int.Parse(HttpContext.Current.Request.QueryString[this.Param]);
            }
            
catch (Exception ex)
            {
                pageid 
= 1//判断异常情况
            }
        }
        
if (content.Contains(this.SpecialString))  //判断是否包含该特殊字符 若果否 直接输出原文章
        {
            
string temp = content.Replace(specialString, "^");  //包含用特殊字符替换,注意这其中存在一个隐患^
            conArrays = temp.Split('^');
            pageSize 
= conArrays.Length;
            pageInfo 
= "<div id=\"LoafPage\">";              //定义自己的div 便于写样式
            pageid = pageid > pageSize ? pageSize : pageid; //如果大于总页数则等于总页数,否则等于当前页
            pageid = pageid < 1 ? 1 : pageid;              //如果小于1则为1  两次过滤 从而保证 正确性
            for (int i = 1; i <= pageSize; i++)
            {

                
if (i == pageid)
                {
                    pageInfo 
+= string.Format("<b>{0}</b>", i);
                }
                
else
                {
                    
#region //判断url链接地址----------------
                    
string url = string.Empty; //定义用于输出的分页码
                    string rawUrl = HttpContext.Current.Request.RawUrl;
                    
if (rawUrl.Contains("?"))
                    {
                        
if (rawUrl.Contains(string.Format("&{0}"this.Param)))
                        {
                            
int len = rawUrl.IndexOf(string.Format("&{0}="this.Param));
                            url 
= rawUrl.Substring(0, len) + string.Format("&{0}="this.Param) + i.ToString();

                        }
                        
else if (rawUrl.Contains(string.Format("{0}="this.Param)))
                        {
                            url 
= rawUrl.Substring(0, rawUrl.IndexOf(string.Format("{0}="this.Param))) + string.Format("{0}="this.Param) + i.ToString();
                        }

                        
else
                        {
                            Regex reg 
= new Regex("^(\\w+)\\?(\\w+)=(\\w+)$");
                            
if (reg.IsMatch(rawUrl))
                            {
                                url 
= rawUrl + string.Format("&{0}={1}"this.Param, i.ToString());
                            }
                            
else
                            {

                                url 
= rawUrl.Substring(0, rawUrl.IndexOf("?")) + string.Format("?{0}={1}"this.Param, i.ToString());
                            }
                        }

                    }
                    
else
                    {
                        url 
= rawUrl + string.Format("?{0}="this.Param) + i.ToString();
                    }
                    
#endregion //--------------------------------
                    pageInfo 
+= string.Format("<a href=\"{0}\">{1}</a>", url, i);
                }
            }
            pageInfo 
+= "</div>";
            lit_pageInfo.Text 
= pageInfo;//输出分页页码 html

            
if (pageid > pageSize)
            {
                
return conArrays[pageSize - 1];
            }
            
else if (pageid < 1)
            {
                
return conArrays[0];
            }
            
else
            {
                
return conArrays[pageid - 1];
            }
        }
        
else
        {
            
return content;
        }

    }
    
#endregion

}

 用的时候直接声明一个类(包含2个构造函数),直接用类的的方法 makePage(string content, Literal lit_pageInfo) 

 样式自己可以随便定义,自己需要就写了一个,可能里面不够严谨,希望大家有错误的地方指正,共同进步~

posted @ 2011-01-18 12:50  lance2008  阅读(434)  评论(0编辑  收藏  举报