老生常谈,回顾并整理一下DataGrid分页控件

·摘要
           素材来自于最近完成的一个.net1.0项目。因为DataGrid自带的分页功能不够完善,所以自己写了个控件,用途是支持DataGrid的自定义分页,主要包含首页、上一页、下一页、尾页;任意跳转页;显示所有页数信息等。 这个分页控件可以提供给任何需要的DataGrid使用。

·设计思路
            为了让这个控件能够适用于每个页面,定义了一个接口ICurrentIndex,ICurrentIndex中定义了绑定方法BindData(),以及两个属性int CurrentIndex和int PageCount。这样,每个需要使用分页控件的页面,除了本身继承自Page外,还必须实现接口ICurrentIndex,而且实现绑定方法BindData()。 当然,如果你的用户控件仅仅给一个DataGrid使用,无须这么麻烦通过接口来实现。

以下就是效果图:


·如何使用
            1)当然把分页控件拉到页面上的DataGrid下面 了。DisplayPageNumber属性决定是显示下拉框还是任意输入,本文不做细节讨论。
<TR>
                    <TD><asp:datagrid id="DataGrid1" runat="server" Height="144px" Width="552px" AllowPaging="True" PageSize="5">
                            <PagerStyle Visible="False"></PagerStyle>
                        </asp:datagrid></TD>
                </TR>
                <TR>
                     <TD><uc1:navigation id="navigation" runat="server" DisplayPageNumber="false"></uc1:navigation></TD>
                </TR>

            2)在页面的CodeBehind(2.0中叫CodeFile)里,实现接口ICurrentIndex
public   class  _Default : System.Web.UI.Page, ICurrentIndex
{……}

            3)实现接口中的BindData方法:public void BindData(),在这个方法中,实现绑定DataGrid之后,把DataGrid的页数PageCount(PageCount=(int)Math.Ceiling( 记录总数recordCount * 1.0 / DataGrid.PageSize);)作为参数,传给用户控件里的方法AddPageCount;同时把PageCount、PageSize、记录总数recordCount,以及DataGird的CurrentPageIndex作为参数传给分页控件的NavigationStateChange()方法。

/// <summary>
  /// BindDataGrid
  /// </summary>
  public void BindData()
  {
   DataTable dt = new DataTable();
   if(Session["datasource"] == null)
   {
    string cmdText = "SELECT top 26 ProductID,ProductName,QuantityPerUnit,UnitPrice FROM Products";
    dt = SQLHelper.FillDataTable(cmdText);

    Session["datasource"] = dt;
   }
   else
   {
    dt = (DataTable)Session["datasource"];
   }
   
   if(dt!= null  && dt.Rows.Count > 0)
   {
    this.recordCount = dt.Rows.Count;
    this.pageCount = (int)Math.Ceiling( this.recordCount * 1.0 / this.DataGrid1.PageSize);
   
    ViewState["RecordCount"] = this.recordCount;
    ViewState["PageCount"] = this.pageCount;

    this.DataGrid1.DataSource = dt.DefaultView;
    this.DataGrid1.DataBind();
   }
   else
   {
    this.DataGrid1.Visible = false;
   }

   this.navigation.AddPageCode(this.pageCount);
   this.navigation.NavigationStateChange(this.pageCount, this.DataGrid1.PageSize, this.recordCount, this.DataGrid1.CurrentPageIndex);
  }


            4)实现接口中的两个属性。
public   int  CurrentIndex
        
{
            
get
            
{
                
return this.DataGrid1.CurrentPageIndex;
            }

            
set
            
{
                
this.DataGrid1.CurrentPageIndex = value;
            }

        }


        
public   int  PageCount
        
{
            
get
            
{
                
return this.pageCount;
            }

        }
   
         5)自此,页面部分的代码已经做完,不算复杂吧。下面开始看看用户控件中的代码:
            6)分页控件中的public void AddPageCode(int pageCount):把页码显示在下拉框中。
public   void  AddPageCode( int  pageCount)
        
{
            
if(this.displayPageNumber)//显示下拉框页码
            {
                
this.txtPageNumber.Visible = false;
                
this.btnGo.Visible = false;
                
this.ddlPageIndex.Items.Clear();//先清除
                for(int i=1;i<= pageCount;i++)
                
{
                    
this.ddlPageIndex.Items.Add(i.ToString());
                }

            }

            
else//否则让用户自己输入页码
            {
                
this.txtPageNumber.Visible = true;
                
this.btnGo.Visible = true;
                
this.ddlPageIndex.Visible = false;
            }

        }

            7)分页控件中的public void NavigationStateChange(int pageCount, int pageSize, int recordCount, int currentIndex)方法:根据传入的参数,控制导航按钮或数字的状态。
///   < summary >
        
///  控制导航按钮或数字的状态
        
///   </ summary >
        public void NavigationStateChange(
int  pageCount ,   int  pageSize ,   int  recordCount ,   int  currentIndex)
        {
            this
. LtlPageCount . Text  =  pageCount . ToString();
            this
. LtlPageIndex . Text  =   " 1 " ;
            this
. LtlPageSize . Text  =  pageSize . ToString();
            this
. LtlRecordCount . Text  =  recordCount . ToString();

            
if ( pageCount  <=   1  ) // ( RecordCount  <=  PageSize ) // 小于等于一页
            {
                this
. LBtnFirst . Enabled  =  false;
                this
. LBtnPrev . Enabled  =  false;
                this
. LBtnNext . Enabled  =  false;
                this
. LBtnLast . Enabled  =  false;
            }
            
else   // 有多页
            {
                
if ( currentIndex  ==   0  ) // 当前为第一页
                {
                    this
. LBtnFirst . Enabled  =  false;
                    this
. LBtnPrev . Enabled  =  false;
                    this
. LBtnNext . Enabled  =  true;
                    this
. LBtnNext . ForeColor  =   System . Drawing . Color . Red;
                    this
. LBtnLast . Enabled  =  true;
                    this
. LBtnLast . ForeColor  =   System . Drawing . Color . Red;
                }
                
else   if (currentIndex  ==  pageCount  -   1  ) // 当前为最后页 
                {
                    this
. LBtnFirst . Enabled  =  true;
                    this
. LBtnFirst . ForeColor  =   System . Drawing . Color . Red;
                    this
. LBtnPrev . Enabled  =  true;
                    this
. LBtnPrev . ForeColor  =   System . Drawing . Color . Red;
                    this
. LBtnNext . Enabled  =  false;
                    this
. LBtnLast . Enabled  =  false;
                }
                
else   // 中间页
                {
                    this
. LBtnFirst . Enabled  =  true;
                    this
. LBtnFirst . ForeColor  =   System . Drawing . Color . Red;
                    this
. LBtnPrev . Enabled  =  true;
                    this
. LBtnPrev . ForeColor  =   System . Drawing . Color . Red;
                    this
. LBtnNext . Enabled  =  true;
                    this
. LBtnNext . ForeColor  =   System . Drawing . Color . Red;
                    this
. LBtnLast . Enabled  =  true;
                    this
. LBtnLast . ForeColor  =   System . Drawing . Color . Red;
                }                  
            }

            
if (recordCount  ==   0 ) // 当没有纪录时DataGrid . PageCount会显示1页
                this
. LtlPageCount . Text  =   " 0 " ;
            
else
                this
. LtlPageCount . Text  =  pageCount . ToString();
            
if (recordCount  ==   0 )
                this
. LtlPageIndex . Text  =   " 0 " ;
            
else
                this
. LtlPageIndex . Text  =  (currentIndex  +   1 ) . ToString(); // 在有页数的情况下前台显示页数加1
            this
. LtlPageSize . Text  =  pageSize . ToString();
            this
. LtlRecordCount . Text  =  recordCount . ToString();

            this
. ddlPageIndex . SelectedIndex  =  currentIndex;
        }

            8)控件中跳转控制代码:主要是设置DataGrid的CurrentIndex,并调用页面中的BindData()方法。
private void btnGo_Click(object sender ,   System . Web . UI . ImageClickEventArgs e)
        {
            
if (this . txtPageNumber . Text . Trim()  !=   "" )
            {
                
int   index   =  Convert . ToInt32(this . txtPageNumber . Text . Trim())  -   1 ;
                
if ( index   <   0 )
                {
                    
index   =   0 ;
                }
                
else   if ( index   >=  Convert . ToInt32(this . LtlPageCount . Text))
                {
                    
index   =  Convert . ToInt32(this . LtlPageCount . Text)  -   1 ;
                }
                this
. txtPageNumber . Text  =  ( index   +   1 ) . ToString();
                this
. iParent . CurrentIndex  =   index ;
                this
. iParent . BindData();
            }
            
else
            {
                this
. iParent . BindData();
                
return ; // this . RegularExpressionValidator1 . IsValid  =  false;
            }
        }

完整代码下载: http://www.cnblogs.com/Files/lxinxuan/DataGridPager.rar
备注:本文引用自 http://www.cnblogs.com/lxinxuan/archive/2007/04/19/719866.html 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值