一个简单的分页控件

using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Text;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.ComponentModel;

namespace KuKu
{
    
/// <summary>
    
/// Summary description for KuKuPager.
    
/// </summary>

    public class KuKuPager:WebControl
    
{

        
//背景图片
        string _backGroud;        
        
//页查询参数
        string _pageQurey;
        
//首页编号
        int _firstPage=1;
        
//总共的记录数目
        int _totalRecord;
        
//页面大小
        int _pageSize;    
        
int _pageIndex;
        
int _totalPage;

        
public KuKuPager()
        
{
                           
                        
        }


        
protected override void OnLoad(EventArgs e)
        
{

            _pageIndex
=_firstPage;
            
if(HttpContext.Current.Request.QueryString[_pageQurey]!=null)
            
{
                _pageIndex
= Convert.ToInt32(HttpContext.Current.Request.QueryString[_pageQurey]);
            }
            
            
if(_pageSize!=0)
            
{
                
if(_totalRecord%_pageSize==0)
                
{
                    _totalPage
= _totalRecord/_pageSize;
                }

                
else
                
{
                    _totalPage
= _totalRecord/_pageSize+1;
                }

            }

            
        }



        
protected override void Render(HtmlTextWriter writer)
        
{
            
string first    = "<font face='webdings'>9</font>";
            
string previous= "<font face='webdings'>7</font>";
            
string next    = "<font face='webdings'>8</font>";
            
string last    = "<font face='webdings'>:</font>";
            StringBuilder sb 
=new StringBuilder();
            sb.Append(
"<div style='float:left'>");            
            sb.Append(
"<font class='t3' style='font-family:Courier New;font-size:12px'>");
            
if(PageIndex==1)
            
{
                sb.Append(first 
+ " " + previous + " <b>");
            }

            
else
            
{
                sb.Append(
"<a href='"+BuildNewPageUrl(this.FirstPageNum)+"'>" + first + "</a> ");
                sb.Append(
"<a href='" + BuildNewPageUrl(this.PageIndex-1)+ "'>" + previous + "</a> <b>");
            }

            
int j=1;
            
if(PageIndex-5>=1)
            
{
                j
=PageIndex-5;
            }

            
int k=TotalPage;
            
if(PageIndex+5<=TotalPage)
            
{
                k
=PageIndex+5;
            }

            
if(j>1)
            
{
                sb.Append(
" <a href='" +BuildNewPageUrl(this.FirstPageNum)+"'>1</a>  ");
            }

            
for(int i=j;i<k+1;i++)
            
{
                
if(PageIndex==i)
                
{
                    sb.Append(
"<span class='t2'>" + i + "</span> ");
                }

                
else
                
{
                    sb.Append(
"<a href='" +BuildNewPageUrl(i-this.FirstPageNum+1)+"'>" + i + "</a> ");
                }
                
            }

            
if(TotalPage>k)
            
{
                sb.Append(
"  <a href='" + BuildNewPageUrl(this.TotalPage)+"'>" + TotalPage + "</a> ");
            }

            
if(PageIndex==TotalPage)
            
{
                sb.Append(
"</b>" + next + " " + last);
            }

            
else
            
{
                sb.Append(
"</b><a href='" +BuildNewPageUrl(this.PageIndex+1)+"'>" + next + "</a> ");
                sb.Append(
"<a href='" + BuildNewPageUrl(this.TotalPage)+"'>" + last + "</a>");
            }

            sb.Append(
"</font></div>");
            
            sb.Append(
"<div style='float:right'>");
            sb.Append(
"[总记录数:<span class='pagerRight'>" + TotalRecord + "</span>]");
            sb.Append(
"[每页:<span class='pagerRight'>" + PageSize + "</span>]");
            sb.Append(
"[总页数:<span class='pagerRight'>" + TotalPage + "</span>]");
            sb.Append(
"[当前为第<span class='pagerRight'>"+PageIndex+"</span>页]</div>");
            
            writer.WriteLine(sb.ToString());
        }

        
/// <summary>
        
/// 当前页面的路径
        
/// </summary>

        string BuildNewPageUrl(int page)
        
{
            
            HttpRequest request
=HttpContext.Current.Request;
            
//页面没有参数
            if(request.QueryString.Count==0)
            
{
                
return request.Url.PathAndQuery+"?"+this.PageQuery+"="+page;                    
            }

                
//如果当前页面没有页面查询参数,说明为第一页
            else if(request.QueryString[this.PageQuery]==null)
            
{
                
return request.Url.PathAndQuery+"&"+this.PageQuery+"="+page;                                                                   
            }

            
else
            
{
                
return request.Url.PathAndQuery.Replace(this.PageQuery+"="+request.QueryString[this.PageQuery],this.PageQuery+"="+page.ToString());                
            }
            
        }
        
        
/// <summary>
        
/// 获取或者设置KuKuPager的背景图片
        
/// </summary>

        public string BackGround
        
{
            
get
            
{
                
return _backGroud;
            }

            
set
            
{
                _backGroud
=value;
            }

        }

        
/// <summary>
        
/// 获取或者设置KuKuPager的页查询参数
        
/// </summary>

        public string PageQuery
        
{
            
get
            
{
                
return _pageQurey;
            }

            
set
            
{
                _pageQurey
=value;
            }

        }

        
/// <summary>
        
/// 获取或者设置KuKuPager首页编号
        
/// </summary>

        public int FirstPageNum
        
{
            
get
            
{
                
return _firstPage;
            }

            
set
            
{
                _firstPage
=value;
            }

        }

        
/// <summary>
        
/// 获取或者设置KuKuPager页面大小
        
/// </summary>

        public int PageSize
        
{
            
get
            
{
                
return _pageSize;
            }

            
set
            
{
                _pageSize
=value;
            }

        }

        
/// <summary>
        
/// 获取或者设置KuKuPager总共的记录数目
        
/// </summary>

        public int TotalRecord
        
{
            
get
            
{
                
return _totalRecord;
            }

            
set
            
{
                _totalRecord
=value;
            }

        }

        
public int PageIndex
        
{
            
get
            
{
                
return _pageIndex;                
            }

        }

        
/// <summary>
        
/// 获取或者设置KuKuPager总页数
        
/// </summary>

        public int TotalPage
        
{
            
get
            
{                
                
return _totalPage;
            }

        }

        
    }

}


posted @ 2006-03-27 08:30  Robin Zhang  阅读(464)  评论(0编辑  收藏  举报