[原创]老生常谈,回顾并整理一下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;
            }
        }

完整代码下载: https://files.cnblogs.com/lxinxuan/DataGridPager.rar
posted @ 2007-04-19 20:22  Kevin Lin  阅读(4199)  评论(19编辑  收藏  举报