一个简单的分页控件

None.gifusing System;
None.gif
using System.Web;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Text;
None.gif
using System.Drawing;
None.gif
using System.Web.UI.HtmlControls;
None.gif
using System.Web.UI;
None.gif
using System.ComponentModel;
None.gif
None.gif
namespace KuKu
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for KuKuPager.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class KuKuPager:WebControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
//背景图片
InBlock.gif
        string _backGroud;        
InBlock.gif        
//页查询参数
InBlock.gif
        string _pageQurey;
InBlock.gif        
//首页编号
InBlock.gif
        int _firstPage=1;
InBlock.gif        
//总共的记录数目
InBlock.gif
        int _totalRecord;
InBlock.gif        
//页面大小
InBlock.gif
        int _pageSize;    
InBlock.gif        
int _pageIndex;
InBlock.gif        
int _totalPage;
InBlock.gif
InBlock.gif        
public KuKuPager()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif                           
InBlock.gif                        
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnLoad(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            _pageIndex
=_firstPage;
InBlock.gif            
if(HttpContext.Current.Request.QueryString[_pageQurey]!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _pageIndex
= Convert.ToInt32(HttpContext.Current.Request.QueryString[_pageQurey]);
ExpandedSubBlockEnd.gif            }
            
InBlock.gif            
if(_pageSize!=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(_totalRecord%_pageSize==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    _totalPage
= _totalRecord/_pageSize;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    _totalPage
= _totalRecord/_pageSize+1;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockEnd.gif        }

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

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

InBlock.gif            
int j=1;
InBlock.gif            
if(PageIndex-5>=1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                j
=PageIndex-5;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
int k=TotalPage;
InBlock.gif            
if(PageIndex+5<=TotalPage)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                k
=PageIndex+5;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if(j>1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sb.Append(
" <a href='" +BuildNewPageUrl(this.FirstPageNum)+"'>1</a> dot.gif ");
ExpandedSubBlockEnd.gif            }

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

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

InBlock.gif            
if(TotalPage>k)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sb.Append(
" dot.gif <a href='" + BuildNewPageUrl(this.TotalPage)+"'>" + TotalPage + "</a> ");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if(PageIndex==TotalPage)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sb.Append(
"</b>" + next + " " + last);
ExpandedSubBlockEnd.gif            }

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

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

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 当前页面的路径
ExpandedSubBlockEnd.gif        
/// </summary>

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

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

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

InBlock.gif        public string BackGround
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _backGroud;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _backGroud
=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取或者设置KuKuPager的页查询参数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string PageQuery
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _pageQurey;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _pageQurey
=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取或者设置KuKuPager首页编号
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public int FirstPageNum
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _firstPage;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _firstPage
=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取或者设置KuKuPager页面大小
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public int PageSize
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _pageSize;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _pageSize
=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取或者设置KuKuPager总共的记录数目
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public int TotalRecord
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _totalRecord;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _totalRecord
=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public int PageIndex
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _pageIndex;                
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取或者设置KuKuPager总页数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public int TotalPage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{                
InBlock.gif                
return _totalPage;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值