快乐生活 轻松工作

分页导航控件
向任何 ASP.NET 控件添加分页功能。

从程序员的角度来看,Microsoft® SQL Server™ 查询的最大缺陷之一就是返回的行数通常比应用程序的用户界面实际可以容纳的行数要多得多。这种尴尬情形经常将开发人员陷于困境。开发人员是应该创建一个非常长的页面,让用户花时间去滚动浏览,还是应该通过设置一个手动分页机制来更好地解决这个问题?

哪种解决方案更好,在很大程度上取决于要检索的数据的特性。由多个项目(如搜索结果)组成的较长列表,最好通过各页大小相等、每页相对较短的多个页面显示。由单个项目(如文章的文本)组成的较长列表,如果整个插入在一个页面中,使用起来会更方便。最后得出的分析结果是,应该根据应用程序的总体用途来做决定。

ASP.NET 提供了功能强大的数据绑定控件,以便将查询结果格式化为 HTML 标记。但是,这些数据绑定控件中只有一种控件(即 DataGrid 控件)本来就支持分页。其他控件(如 DataListRepeaterCheckBoxList)则不支持分页。这些控件及其他列表控件不支持分页,不是因为它们在结构上不支持分页,而是因为它们与 DataGrid 不同,不包含任何处理分页的特定代码。为使况且DataGrid控件支持分页功能就必须是的他的ViewStatue为True,而有时我们没有必要开启ViewSatue.我们知道处理分页的代码是相当样板化的,所以可以添加该功能到所有这些控件中。

当前流行的分页格式有【第一页 上一页 下一页 最后一页 跳到第『』页】其实现方法如下

namespace jewel
{
 
using System;
 
using System.Data;
 
using System.Drawing;
 
using System.Web;
 
using System.Web.UI.WebControls;
 
using System.Web.UI.HtmlControls;

 
/// <summary>
 
///  Summary description for pagenavigation.
 
/// </summary>

 public class pagenavigation : System.Web.UI.UserControl
 
{
  
protected System.Web.UI.WebControls.Label lab_itemcount;
  
protected System.Web.UI.WebControls.Label lab_pagecount;
  
protected System.Web.UI.WebControls.Label lab_pageindex;
  
protected System.Web.UI.WebControls.LinkButton bnt_first;
  
protected System.Web.UI.WebControls.LinkButton bnt_prev;
  
protected System.Web.UI.WebControls.LinkButton bnt_next;
  
protected System.Web.UI.WebControls.LinkButton bnt_last;
  
protected System.Web.UI.WebControls.TextBox txt_gopage;
  
protected System.Web.UI.WebControls.LinkButton bnt_go;

  
public delegate void PageNavigationHandler(Object sender,EventArgs args);
  
public event PageNavigationHandler PageChange;   //定义页面发生变化的事件。
  protected virtual void OnPageChange(EventArgs e){
   
if(PageChange!=null){
    PageChange(
this,e);
   }

  }

  
public int ItemCount{                    ////记录总数
   get{
    
if(ViewState["itemcount"]==null){
     ViewState[
"itemcount"]=0;
    }

    
return int.Parse(ViewState["itemcount"].ToString());
   }

   
set{ViewState["itemcount"]=value;}
  }

  
public int PageIndex{               //当前页面的页数
   get{
    
if(ViewState["pageindex"]==null){
     ViewState[
"pageindex"]=1;     
    }

    
if(PageCount!=0){
     
if(int.Parse(ViewState["pageindex"].ToString())>PageCount){
      ViewState[
"pageindex"]=PageCount;
     }

    }
else{
     ViewState[
"pageindex"]=1;
    }

    
return int.Parse(ViewState["pageindex"].ToString());
   }

   
set{ViewState["pageindex"]=value;}
  }

  
public int PageSize{             //每页显示记录的数量
   get{
    
if(ViewState["pagesize"]==null){
     ViewState[
"pagesize"]=8;
    }

    
return int.Parse(ViewState["pagesize"].ToString());}

   
set{ViewState["pagesize"]=value;}
  }

  
private int PageCount{
   
get{
    
int pagecount;
    
if(ItemCount%PageSize==0){
     pagecount
=ItemCount/PageSize;
    }
else{
     pagecount
=ItemCount/PageSize+1;
    }

    
return pagecount;
   }

  }


  
private void Page_Load(object sender, System.EventArgs e) {   
  }
  
  
public void setdisplay(){
   
this.bnt_first.Enabled=true;
   
this.bnt_prev.Enabled=true;  
   
this.bnt_next.Enabled=true;
   
this.bnt_last.Enabled=true;   
   
this.lab_itemcount.Text=ItemCount.ToString();
   
this.lab_pagecount.Text=PageCount.ToString();
   
this.lab_pageindex.Text=PageIndex.ToString(); 
   
if(PageCount==0){
    
this.bnt_first.Enabled=false;
    
this.bnt_prev.Enabled=false;  
    
this.bnt_next.Enabled=false;
    
this.bnt_last.Enabled=false;
    
this.bnt_go.Enabled=false;
    
return;
   }

   
if(PageIndex==1){
    
this.bnt_first.Enabled=false;
    
this.bnt_prev.Enabled=false;    
   }

   
if(PageIndex==PageCount){    
    
this.bnt_next.Enabled=false;
    
this.bnt_last.Enabled=false;
   }

  }

  

  
Web 窗体设计器生成的代码

  
private void bnt_first_Click(object sender, System.EventArgs e) {
   PageIndex
=1;
   OnPageChange(e);
   setdisplay();
  }


  
private void bnt_prev_Click(object sender, System.EventArgs e) {
   PageIndex
=PageIndex-1;
   OnPageChange(e);
   setdisplay();
  }


  
private void bnt_next_Click(object sender, System.EventArgs e) {
   PageIndex
=PageIndex+1;
   OnPageChange(e);
   setdisplay();
  }


  
private void bnt_last_Click(object sender, System.EventArgs e) {
   
int pagecount;
   
if(ItemCount%PageSize==0){
    pagecount
=ItemCount/PageSize;
   }
else{
    pagecount
=ItemCount/PageSize+1;
   }

   PageIndex
=pagecount;
   OnPageChange(e);
   setdisplay();
  }


  
private void bnt_go_Click(object sender, System.EventArgs e) {
   
int gopage=int.Parse(this.txt_gopage.Text);
   
if(gopage<1){
    PageIndex
=1;
   }
else{
    
int pagecount;
    
if(ItemCount%PageSize==0){
     pagecount
=ItemCount/PageSize;
    }
else{
     pagecount
=ItemCount/PageSize+1;
    }

    
if(pagecount<gopage){
     PageIndex
=pagecount;
    }
else{
     PageIndex
=gopage;
    }

   }

   OnPageChange(e);
   setdisplay();
  }

 }

}



还有另一种格式【page 2/10  1 2 3 4 5 >】在原来的基础上添加功能就能实现
namespace jewel
{
    
using System;
    
using System.Text;
    
using System.Data;
    
using System.Drawing;
    
using System.Web;
    
using System.Web.UI;
    
using System.Web.UI.WebControls;
    
using System.Web.UI.HtmlControls;

    
/// <summary>
    
///        Summary description for pagenavigation.
    
/// </summary>

    public class pagenavigation : System.Web.UI.UserControl,IPostBackEventHandler
    
{
        
protected System.Web.UI.WebControls.LinkButton linknext;
        
protected System.Web.UI.WebControls.Label lab_pageexplain;
        
protected System.Web.UI.WebControls.LinkButton linkprev;
        
protected System.Web.UI.HtmlControls.HtmlGenericControl span_navigation;
        
private const int displaycount=5;     //每页显示的页面导航页数(1 2 3 4 5)   

        
public delegate void PageNavigationHandler(Object sender,EventArgs args);
        
public event PageNavigationHandler PageChange;          //定义页面发生变化的事件。
        protected virtual void OnPageChange(EventArgs e){
            
if(PageChange!=null){
                PageChange(
this,e);
            }

        }

        
public int ItemCount{               //记录总数
            get{
                
if(ViewState["itemcount"]==null){
                    ViewState[
"itemcount"]=0;
                }

                
return int.Parse(ViewState["itemcount"].ToString());
            }

            
set{ViewState["itemcount"]=value;}
        }

        
public int PageIndex{           //当前页面的页数
            get{
                
if(ViewState["pageindex"]==null){
                    ViewState[
"pageindex"]=1;                    
                }

                
if(PageCount!=0){
                    
if(int.Parse(ViewState["pageindex"].ToString())>PageCount){
                        ViewState[
"pageindex"]=PageCount;
                    }

                }
else{
                    ViewState[
"pageindex"]=1;
                }

                
return int.Parse(ViewState["pageindex"].ToString());
            }

            
set{ViewState["pageindex"]=value;}
        }

        
public int DisplayIndex{          //当前页面所属的导航页的页数(如果当前页面页数是3,页面导航显示的是1 2 3 4 5,那么DisplayIndex=1  如果当前页面页数是7,页面导航显示的是6 7 8 9 10,那么DisplayIndex=2)
            get{
                
if(ViewState["displayindex"]==null){
                    ViewState[
"displayindex"]=1;
                }

                
if(DisplayCount!=0){
                    
if(int.Parse(ViewState["displayindex"].ToString())>DisplayCount){
                        ViewState[
"displayindex"]=DisplayCount;
                    }

                }
else{
                    ViewState[
"displayindex"]=1;
                }

                
return int.Parse(ViewState["displayindex"].ToString());
            }

            
set{ViewState["displayindex"]=value;}
        }

        
public int DisplayCount{      //总共页面导航数
            get{
                
int displayitem;
                
if(ItemCount%(PageSize*displaycount)==0){
                    displayitem
=ItemCount/(PageSize*displaycount);
                }
else{
                    displayitem
=ItemCount/(PageSize*displaycount)+1;
                }

                
return displayitem;
            }

        }

        
public int DisplaySize{      //每页显示的页面导航页数
            get{
                
if(DisplayIndex==DisplayCount){
                    
return PageCount-(DisplayIndex-1)*displaycount;
                }

                
return displaycount;
            }

        }

        
public int PageSize{           //每页显示记录的数量
            get{
                
if(ViewState["pagesize"]==null){
                    ViewState[
"pagesize"]=8;
                }

                
return int.Parse(ViewState["pagesize"].ToString());}

            
set{ViewState["pagesize"]=value;}
        }

        
private int PageCount{         //页面数量
            get{
                
int pagecount;
                
if(ItemCount%PageSize==0){
                    pagecount
=ItemCount/PageSize;
                }
else{
                    pagecount
=ItemCount/PageSize+1;
                }

                
return pagecount;
            }

        }


        
private void Page_Load(object sender, System.EventArgs e) {            
        }
        
        
private string displaynavigation(){
            StringBuilder navigationstr
=new StringBuilder();
            
if(ItemCount==0 || ItemCount==1){
                lab_pageexplain.Visible
=false;
                
return "";
            }

            
int pagesign;
            
for(int i=1;i<=DisplaySize;i++){            
                pagesign
=(DisplayIndex-1)*displaycount+i;
                
if(PageIndex==pagesign){
                    navigationstr.Append(
"&nbsp;").Append(pagesign).Append("&nbsp;");
                }
else{
                    navigationstr.Append(
"&nbsp;<a href=\"javascript:").Append(Page.GetPostBackEventReference(this,pagesign.ToString()));
                    navigationstr.Append("\">");
                    navigationstr.Append(pagesign.ToString());
                    navigationstr.Append(
"</a>&nbsp;");    
                }

            }
            
            
return navigationstr.ToString();
        }

        
public void RaisePostBackEvent(string eventArgument) {
            PageIndex
=int.Parse(eventArgument);
            EventArgs e
=new EventArgs();
            OnPageChange(e);
            setdisplay();
        }

        
public void setdisplay(){
            span_navigation.InnerHtml
=displaynavigation();
            
this.lab_pageexplain.Text=String.Format("page {0}/{1}",PageIndex,PageCount);
            
this.linkprev.Visible=true;
            
this.linknext.Visible=true;
            
if(DisplayIndex==1){
                
this.linkprev.Visible=false;
            }

            
if(DisplayIndex==DisplayCount){
                
this.linknext.Visible=false;
            }

        }

        

        
Web 窗体设计器生成的代码




        
private void linkprev_Click(object sender, System.EventArgs e) {
            DisplayIndex
=DisplayIndex-1;
            PageIndex
=(DisplayIndex-1)*displaycount+1;            
            OnPageChange(e);
            setdisplay();
        }


        
private void linknext_Click(object sender, System.EventArgs e) {
            DisplayIndex
=DisplayIndex+1;
            PageIndex
=(DisplayIndex-1)*displaycount+1;                    
            OnPageChange(e);
            setdisplay();
        }

    }

}

posted on 2006-02-22 10:08  思考 学习 总结  阅读(263)  评论(0)    收藏  举报