ASP.NET GridView,DataList,Repeater的通用分页


None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
namespace  ustonetdonet.DotNetPagedData
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**////  <summary>
InBlock.gif    
///  本类提供GridView,DataList,Repeater的通用分页
InBlock.gif    
///  参考了NetFire(Fire.Rolland.Han)http://netfire.my.gsdn.net/2005/04/04/2945/
InBlock.gif    
///  
InBlock.gif    
///  
InBlock.gif    
///     使用方法:
InBlock.gif    
///     1.在引用里添加dll的引用
InBlock.gif    
///  2.using ustonetdonet.DotNetPagedData;
InBlock.gif    
///  3.private DotNetPagedData pager;
InBlock.gif    
///  4.page_load()
InBlock.gif    
///  if(!IsPostBack)   
InBlock.gif    
///  {    
InBlock.gif    
///     pager=new DotNetPagedData();
InBlock.gif    
///     pager.PagedPanel=this.pagedPanel;    
InBlock.gif    
///     pager.GridView = this.GridView1;  //如果是DataGrid则添加此项
InBlock.gif    
///     pager.Datalist=this.DataList1;    //如果是DataList则添加此项  
InBlock.gif    
///     pager.Repeater=this.Repeater1;    //如果是Repeater则添加此项  
InBlock.gif    
///     pager.Lnknext=this.lnkNext;    
InBlock.gif    
///        pager.Lnkprev=this.lnkPrev;    
InBlock.gif    
///        pager.Lnklast=this.lnklast;    
InBlock.gif    
///        pager.Lnkfist=this.lnkfist;    
InBlock.gif    
///        pager.Pagenum=this.Pagenum;    
InBlock.gif    
///        pager.Pagecount=this.Pagecount;    
InBlock.gif    
///        pager.List=this.ddl_jump; 
InBlock.gif    
///        //简单示例定义DataSet外部定义,同样也可以采用执行sqlstr,并填充给DataSet,本类也重载了datapage(),详细信息可查看详细代码       
InBlock.gif    
///     DataSet ds=new DataSet();    
InBlock.gif    
///     ds.ReadXml(Server.MapPath("test.xml"));    
InBlock.gif    
///     pager.datapage(ds,5);   
InBlock.gif    
///  }  
InBlock.gif    
///  
InBlock.gif    
///  // DropDownList必须设定为AutoPostBack,下面添加它的事件
InBlock.gif    
///  5.private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)  
InBlock.gif    
///  {      
InBlock.gif    
///     pager=new DotNetPagedData();   
InBlock.gif    
///     pager.List=this.DropDownList1;   
InBlock.gif    
///     pager.Lnkjump=this.HyperLink3;   
InBlock.gif    
///     pager.select();              
InBlock.gif    
///  }
ExpandedSubBlockEnd.gif    
///  </summary>

InBlock.gif
InBlock.gif
InBlock.gif    
public class DotNetPagedData : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private System.Web.UI.WebControls.GridView gridview = null;
InBlock.gif        
private System.Web.UI.WebControls.DataList datalist = null;
InBlock.gif        
private System.Web.UI.WebControls.Repeater repeater = null;
InBlock.gif        
private System.Web.UI.WebControls.HyperLink lnknext;
InBlock.gif        
private System.Web.UI.WebControls.HyperLink lnkprev;
InBlock.gif        
private System.Web.UI.WebControls.HyperLink lnkfist;
InBlock.gif        
private System.Web.UI.WebControls.HyperLink lnklast;
InBlock.gif        
private System.Web.UI.WebControls.HyperLink lnkjump;
InBlock.gif        
private System.Web.UI.WebControls.Label pagenum;
InBlock.gif        
private System.Web.UI.WebControls.Label pagecount;
InBlock.gif        
private System.Web.UI.WebControls.Panel pagedPanel;
InBlock.gif        
private System.Web.UI.WebControls.DropDownList list;
InBlock.gif
InBlock.gif        
public DotNetPagedData()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//  
InBlock.gif            
// TODO: 在此处添加构造函数逻辑   
InBlock.gif            
//  
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public System.Web.UI.WebControls.GridView GridView
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.gridview;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public System.Web.UI.WebControls.DataList Datalist
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.datalist;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public System.Web.UI.WebControls.Repeater Repeater
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.repeater;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public System.Web.UI.WebControls.HyperLink Lnkprev
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.lnkprev;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public System.Web.UI.WebControls.HyperLink Lnknext
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.lnknext;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public System.Web.UI.WebControls.HyperLink Lnkfist
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.lnkfist;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public System.Web.UI.WebControls.HyperLink Lnklast
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.lnklast;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public System.Web.UI.WebControls.HyperLink Lnkjump
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.lnkjump;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public System.Web.UI.WebControls.Label Pagenum
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.pagenum;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public System.Web.UI.WebControls.Label Pagecount
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.pagecount;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public System.Web.UI.WebControls.DropDownList List
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.list;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public System.Web.UI.WebControls.Panel PagedPanel
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.pagedPanel;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//针对一般数据邦定,不执行sqlstr
InBlock.gif
        public void datapage(DataSet ds, int pagenum)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            PagedDataSource objPds 
= new PagedDataSource();
InBlock.gif            objPds.AllowPaging 
= true;
InBlock.gif            
//每页显示数目   
InBlock.gif
            objPds.PageSize = pagenum;
InBlock.gif            
//指定数据源   
InBlock.gif
            objPds.DataSource = ds.Tables[0].DefaultView;
InBlock.gif            
int CurPage;
InBlock.gif            
//绑定dropdownlist数据   
InBlock.gif
            for (int i = 1; i <= objPds.PageCount; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.list.Items.Add(i.ToString());
ExpandedSubBlockEnd.gif            }

InBlock.gif            
this.list.Items.Insert(0new ListItem("页数"""));
InBlock.gif            
//确定当前页数   
InBlock.gif
            if (System.Web.HttpContext.Current.Request.Params["Page"!= null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                CurPage 
= Convert.ToInt32(System.Web.HttpContext.Current.Request.Params["Page"]);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                CurPage 
= 1;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            objPds.CurrentPageIndex 
= CurPage - 1;
InBlock.gif            
if (objPds.PageCount > 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                pagedPanel.Visible 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                pagedPanel.Visible 
= false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//显示当前页数   
InBlock.gif
            this.pagenum.Text = "当前:" + CurPage.ToString() + "";
InBlock.gif            
//显示总共页数   
InBlock.gif
            this.pagecount.Text = "共有" + objPds.PageCount + "";
InBlock.gif            
//下一页   
InBlock.gif
            if (!objPds.IsLastPage)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lnknext.NavigateUrl 
= System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//上一页   
InBlock.gif
            if (!objPds.IsFirstPage)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lnkprev.NavigateUrl 
= System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//第一页   
InBlock.gif
            if (CurPage != 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lnkfist.NavigateUrl 
= System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//最后一页   
InBlock.gif
            if (CurPage != objPds.PageCount)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lnklast.NavigateUrl 
= System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(objPds.PageCount);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//绑定gridview   
InBlock.gif
            if (this.gridview != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                gridview.DataSource 
= objPds;
InBlock.gif                gridview.DataBind();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//绑定datalist   
InBlock.gif
            if (this.datalist != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                datalist.DataSource 
= objPds;
InBlock.gif                datalist.DataBind();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//绑定repeater   
InBlock.gif
            if (this.repeater != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                repeater.DataSource 
= objPds;
InBlock.gif                repeater.DataBind();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//重载,针对执行sqlstr
InBlock.gif
        public void datapage(string sqlcon, string sqlstr, int pagenum)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlConnection objConn 
= new SqlConnection(sqlcon);
InBlock.gif            SqlDataAdapter objCommand 
= new SqlDataAdapter(sqlstr, objConn);
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif            objCommand.Fill(ds);
InBlock.gif            PagedDataSource objPds 
= new PagedDataSource();
InBlock.gif            objPds.AllowPaging 
= true;
InBlock.gif            
//每页显示数目   
InBlock.gif
            objPds.PageSize = pagenum;
InBlock.gif            
//指定数据源   
InBlock.gif
            objPds.DataSource = ds.Tables[0].DefaultView;
InBlock.gif            
int CurPage;
InBlock.gif            
//绑定dropdownlist数据   
InBlock.gif
            for (int i = 1; i <= objPds.PageCount; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.list.Items.Add(i.ToString());
ExpandedSubBlockEnd.gif            }

InBlock.gif            
this.list.Items.Insert(0new ListItem("页数"""));
InBlock.gif            
//确定当前页数   
InBlock.gif
            if (System.Web.HttpContext.Current.Request.Params["Page"!= null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                CurPage 
= Convert.ToInt32(System.Web.HttpContext.Current.Request.Params["Page"]);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                CurPage 
= 1;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            objPds.CurrentPageIndex 
= CurPage - 1;
InBlock.gif            
if (objPds.PageCount > 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                pagedPanel.Visible 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                pagedPanel.Visible 
= false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//显示当前页数   
InBlock.gif
            this.pagenum.Text = "当前:" + "" + CurPage.ToString() + "";
InBlock.gif            
//显示总共页数   
InBlock.gif
            this.pagecount.Text = "共有" + objPds.PageCount + "";
InBlock.gif            
//下一页   
InBlock.gif
            if (!objPds.IsLastPage)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lnknext.NavigateUrl 
= System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//上一页   
InBlock.gif
            if (!objPds.IsFirstPage)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lnkprev.NavigateUrl 
= System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//第一页   
InBlock.gif
            if (CurPage != 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lnkfist.NavigateUrl 
= System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//最后一页   
InBlock.gif
            if (CurPage != objPds.PageCount)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lnklast.NavigateUrl 
= System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(objPds.PageCount);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//绑定gridview   
InBlock.gif
            if (this.gridview != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                gridview.DataSource 
= objPds;
InBlock.gif                gridview.DataBind();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//绑定datalist   
InBlock.gif
            if (this.datalist != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                datalist.DataSource 
= objPds;
InBlock.gif                datalist.DataBind();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//绑定repeater   
InBlock.gif
            if (this.repeater != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                repeater.DataSource 
= objPds;
InBlock.gif                repeater.DataBind();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//dropdownlist选择改变  
InBlock.gif
        public void select()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.lnkjump.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(this.list.SelectedItem.Value);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
posted on 2007-08-03 22:00 江宇旋 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/jiangyuxuan/archive/2007/08/03/842349.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值