写了个分页导航控件

        经常要用到分页,就自己用ASP.NET2.0随便写了个分页控件,暂时根据项目需要凑合着用下,不足之处切莫见笑
        我的控件直接从CompositeControl继承,不与数据列表控件如RepeaterList等绑定,默认从配置文件自动设置页面大小,从数据库读取当前记录总数,也能自己在初始化时进行设置,主要功能是设置自身导航界面的状态,向外提供该次分页的数据信息:记录总数、当前页码、页面总数、当前要绑定数据的起始记录号(与SQL Server 2005的ROW_NUMBER() OVER使用很方便),从而通过它的属性从外部读出数据绑定到任一可绑定数据控件,代码如下:
using  System;
using  System.Configuration;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Text;
using  System.Web;
using  System.Data;
using  System.Data.SqlClient;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  Charly.DBUtility;    //  数据访问组件引用

namespace  Charly.WebControls
{
    
/// <summary>
    
/// 数据导航控件
    
/// </summary>

    public class DataNavigator : CompositeControl
    
{
        
public delegate void PageIndexChangedEventHandler(DataNavigator dn); 

        
private LinkButton lbFirstPage, lbPreviewPage, lbNextPage, lbLastPage;
        
private TextBox txtPageIndex;
        
private Label lblPageInfor;
        
private Button btnGotoPage;

        
/// <summary>
        
/// 总记录数
        
/// </summary>

        public int RecorderNumber
        
{
            
get
            
{
                
if (ViewState["RecorderNumber"== null)
                
{
                    ViewState[
"RecorderNumber"= -1;
                }

                
return Convert.ToInt32(ViewState["RecorderNumber"]);
            }

            
set
            
{
                ViewState[
"RecorderNumber"= value;
            }

        }


        
/// <summary>
        
/// 当前数据显示单位,如文章则可设置为篇,图片则可设置为个
        
/// </summary>

        public string ObjectQuantifier
        
{
            
get
            
{
                
return ViewState["ObjectQuantifier"].ToString();
            }

            
set
            
{
                ViewState[
"ObjectQuantifier"= value;
            }

        }


        
/// <summary>
        
/// 当前数据显示名称,如文章,图片
        
/// </summary>

        public string ObjectName
        
{
            
get
            
{
                
return ViewState["ObjectName"].ToString();
            }

            
set
            
{
                ViewState[
"ObjectName"= value;
            }

        }


        
/// <summary>
        
/// 当前页码
        
/// </summary>

        public int PageIndex
        
{
            
get
            
{
                
if (ViewState["PageIndex"== null)
                
{
                    ViewState[
"PageIndex"= 1;
                }

                
return Convert.ToInt32(ViewState["PageIndex"]);
            }

            
set
            
{
                ViewState[
"PageIndex"= value;
            }

        }


        
/// <summary>
        
/// 页大小,即每页显示记录数
        
/// </summary>

        public int PageSize
        
{
            
get
            
{
                
if (ViewState["PageSize"== null)
                
{
                    ViewState[
"PageSize"= Convert.ToInt32(ConfigurationManager.AppSettings["UserPageSize"]);
                }

                
return Convert.ToInt32(ViewState["PageSize"]);
            }

            
set
            
{
                ViewState[
"PageSize"= value;
            }

        }


        
/// <summary>
        
/// 总页数
        
/// </summary>

        public int PageNumber
        
{
            
get
            
{
                
return Convert.ToInt32(ViewState["PageNumber"]);
            }

            
set
            
{
                ViewState[
"PageNumber"= value;
            }

        }


        
/// <summary>
        
/// 当前页面数据起始行号
        
/// </summary>

        public int StartRowNumber
        
{
            
get
            
{
                
if (ViewState["StartRowNumber"== null)
                
{
                    ViewState[
"StartRowNumber"= 1;
                }

                
return Convert.ToInt32(ViewState["StartRowNumber"]);
            }

            
set
            
{
                ViewState[
"StartRowNumber"= value;
            }

        }


        
/// <summary>
        
/// 当前页面数据结束行号
        
/// </summary>

        public int EndRowNumber
        
{
            
get
            
{
                
if (ViewState["EndRowNumber"== null)
                
{
                    ViewState[
"EndRowNumber"= PageSize;
                }

                
return Convert.ToInt32(ViewState["EndRowNumber"]);
            }

            
set
            
{
                ViewState[
"EndRowNumber"= value;
            }

        }


        
/// <summary>
        
/// 读取总记录数的表名,为默认读取总记录数提供支持,否则需要在外部设置总记录数
        
/// </summary>

        public string TableName
        
{
            
get
            
{
                
return ViewState["TableName"].ToString();
            }

            
set
            
{
                ViewState[
"TableName"= value;
            }

        }


        
/// <summary>
        
/// 读取总记录数的过滤条件,为默认读取总记录数提供支持,否则需要在外部设置总记录数
        
/// </summary>

        public string Filter
        
{
            
get
            
{
                
if (ViewState["Filter"== null)
                
{
                    ViewState[
"Filter"= string.Empty;
                }

                
return ViewState["Filter"].ToString();
            }

            
set
            
{
                ViewState[
"Filter"= value;
            }

        }


        
/// <summary>
        
/// 翻页事件定义
        
/// </summary>

        public event PageIndexChangedEventHandler PageIndexChanged;


        
/// <summary>
        
/// 创建翻页子控件
        
/// </summary>

        protected override void CreateChildControls()
        
{
            lblPageInfor 
= new Label();
            
this.Controls.Add(lblPageInfor);

            lbFirstPage 
= new LinkButton();
            lbFirstPage.Text 
= "首页";
            lbFirstPage.Click 
+= new EventHandler(GotoFirstPage);
            lbFirstPage.EnableViewState 
= true;
            
this.Controls.Add(lbFirstPage);

            lbPreviewPage 
= new LinkButton();
            lbPreviewPage.Text 
= "上页";
            lbPreviewPage.Click 
+= new EventHandler(GotoPreviewPage);
            
this.Controls.Add(lbPreviewPage);

            lbNextPage 
= new LinkButton();
            lbNextPage.Text 
= "下页";
            lbNextPage.Click 
+= new EventHandler(GotoNextPage);
            
this.Controls.Add(lbNextPage);

            lbLastPage 
= new LinkButton();
            lbLastPage.Text 
= "末页";
            lbLastPage.Click 
+= new EventHandler(GotoLastPage);
            
this.Controls.Add(lbLastPage);

            txtPageIndex 
= new TextBox();
            txtPageIndex.Attributes.Add(
"onkeyup"@"value=value.replace(/[^\d]/g,'');");
            txtPageIndex.Attributes.Add(
"onbeforepaste"@"clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d.]/g,''))");
            
this.Controls.Add(txtPageIndex);

            btnGotoPage 
= new Button();
            btnGotoPage.Text 
= "GO";
            btnGotoPage.CommandName 
= "Go";
            btnGotoPage.Click 
+= new EventHandler(GotoPageIndex);
            
this.Controls.Add(btnGotoPage);

            
base.CreateChildControls();
            SetNavigatorPage();
        }


        
/// <summary>
        
/// 转到首页
        
/// </summary>

        protected void GotoFirstPage(object sender, EventArgs e)
        
{
            
if (PageIndexChanged != null)
            
{
                PageIndex 
= 1;
                SetNavigatorPage();
                PageIndexChanged(
this);
            }

        }


        
/// <summary>
        
/// 转到上一页
        
/// </summary>

        protected void GotoPreviewPage(object sender, EventArgs e)
        
{
            
if (PageIndexChanged != null)
            
{
                PageIndex 
--;
                SetNavigatorPage();
                PageIndexChanged(
this);
            }

        }


        
/// <summary>
        
/// 转到下一页
        
/// </summary>

        protected void GotoNextPage(object sender, EventArgs e)
        
{
            
if (PageIndexChanged != null)
            
{
                PageIndex
++;
                SetNavigatorPage();
                PageIndexChanged(
this);
            }

        }


        
/// <summary>
        
/// 转到末页
        
/// </summary>

        protected void GotoLastPage(object sender, EventArgs e)
        
{
            
if (PageIndexChanged != null)
            
{
                PageIndex 
= PageNumber;
                SetNavigatorPage();
                PageIndexChanged(
this);
            }

        }


        
/// <summary>
        
/// 转到任一页
        
/// </summary>

        protected void GotoPageIndex(object sender, EventArgs e)
        
{
            
if (PageIndexChanged != null)
            
{
                
int iInputPageIndex;
                
if (PageIndexChanged != null && System.Int32.TryParse(txtPageIndex.Text.Trim(), out iInputPageIndex))
                
{
                    
if (iInputPageIndex != PageIndex)
                    
{
                        PageIndex 
= iInputPageIndex;
                        SetNavigatorPage();
                        PageIndexChanged(
this);
                    }

                }

            }

        }


        
/// <summary>
        
/// 初始化导航控件
        
/// </summary>

        public void SetNavigatorPage()
        
{
            
int iRecordNumber;
            
if (RecorderNumber == -1)
            
{
                
// 从数据库读取总记录数
                SqlHandler sh = new SqlHandler();
                sh.CommandText 
= "GetObjectNumber";
                sh.SetCommandParameter(
"@TableName", SqlDbType.VarChar, 50, TableName);
                sh.SetCommandParameter(
"@Filter", SqlDbType.NVarChar, 100, Filter);
                iRecordNumber 
= Convert.ToInt32(sh.ExecuteScalar());
            }

            
else
            
{
                
// 读取外部设置总记录数
                iRecordNumber = RecorderNumber;
            }


            
if (iRecordNumber == 0)
            
{
                lblPageInfor.Text 
= "还没有" + ObjectName;
                lbFirstPage.Visible 
= false;
                lbPreviewPage.Visible 
= false;
                lbNextPage.Visible 
= false;
                lbLastPage.Visible 
= false;
                txtPageIndex.Visible 
= false;
                btnGotoPage.Visible 
= false;
            }

            
else
            
{
                lbFirstPage.Visible 
= true;
                lbPreviewPage.Visible 
= true;
                lbNextPage.Visible 
= true;
                lbLastPage.Visible 
= true;
                txtPageIndex.Visible 
= true;
                btnGotoPage.Visible 
= true;

                
// 计算总页数
                if (iRecordNumber % PageSize == 0)
                    PageNumber 
= iRecordNumber / PageSize;
                
else
                    PageNumber 
= iRecordNumber / PageSize + 1;

                
// 设置当前页码
                if (PageIndex < 1)
                    PageIndex 
= 1;
                
else
                
{
                    
if (PageIndex > PageNumber && PageNumber != 0)
                        PageIndex 
= PageNumber;
                }


                
// 设置翻页状态
                if (PageNumber == 1)
                
{
                    lbFirstPage.Enabled 
= false;
                    lbPreviewPage.Enabled 
= false;
                    lbNextPage.Enabled 
= false;
                    lbLastPage.Enabled 
= false;
                    txtPageIndex.Enabled 
= false;
                    btnGotoPage.Enabled 
= false;
                }

                
else
                
{
                    
if (PageIndex == 1)
                    
{
                        lbFirstPage.Enabled 
= false;
                        lbPreviewPage.Enabled 
= false;
                        lbNextPage.Enabled 
= true;
                        lbLastPage.Enabled 
= true;
                    }

                    
else
                    
{
                        
if (PageIndex == PageNumber)
                        
{
                            lbFirstPage.Enabled 
= true;
                            lbPreviewPage.Enabled 
= true;
                            lbNextPage.Enabled 
= false;
                            lbLastPage.Enabled 
= false;
                        }

                        
else
                        
{
                            lbFirstPage.Enabled 
= true;
                            lbPreviewPage.Enabled 
= true;
                            lbNextPage.Enabled 
= true;
                            lbLastPage.Enabled 
= true;
                        }

                    }

                }


                StartRowNumber 
= PageSize * (PageIndex - 1+ 1;
                
int iRemainder = iRecordNumber % PageSize;
                
if (iRemainder == 0)
                
{
                    EndRowNumber 
= StartRowNumber + PageSize - 1;
                }

                
else
                
{
                    
if (PageIndex == PageNumber)        // 要显示最后一页
                    {
                        EndRowNumber 
= StartRowNumber + iRemainder - 1;
                    }

                    
else
                    
{
                        EndRowNumber 
= StartRowNumber + PageSize - 1;
                    }

                }


                
// 设置控件状态
                lblPageInfor.Text = "共有" + iRecordNumber.ToString() + ObjectQuantifier + ObjectName + " 第" + PageIndex.ToString() + "/" + PageNumber.ToString() + "";
                txtPageIndex.Text 
= PageIndex.ToString();
            }

        }

    }

}



        上面获取记录总数的存储过程如下:
CREATE   PROCEDURE  dbo.GetObjectNumber
(
    
@TableName   varchar ( 50 ),
    
@Filter   nvarchar ( 100 )
)

AS
    
    
DECLARE   @spSQL   varchar ( 500 )
    
SET   @spSQL   =   ' SELECT COUNT(*) AS RecordNumber FROM  '   +   @TableName   +   '  WHERE 1=1  '   +   @Filter
    
EXEC ( @spSQL )

RETURN

        该控件使用如下:
        1.添加对该控件dll的引用,并在页面中添加引用:
<% @ Register TagPrefix = "Charly "  Namespace = "Charly .WebControls "  Assembly = " WebControls "   %>
        2.在你的Page_Load中绑定翻页事件方法,并设置好它的相关属性,如
dnArticleList.PageIndexChanged  +=   new  DataNavigator.PageIndexChangedEventHandler(DataNavigatorArticleList_PageIndexChanged);
    dnArticleList.ObjectName = "文章";
    dnArticleList.ObjectQuantifier = "篇";
    dnArticleList.TableName = "Articles";
    dnArticleList.Filter =  " AND UserID = '" + strUserID + "'";
    dnArticleList.PageIndex = 1;
        在该DataNavigatorArticleList_PageIndexChanged方法中重新绑定你要显示的列表数据。
        3.控件初始化
        根据自己的需要在初始化的时候(一般是Page_Load中)对导航控件的属性进行设置,如需要重新设置,可设置完其属性后,调用它的SetNavigatorPage()方法生效,比如重新设定过滤条件的时候。
        翻页的时候需要根据它的属性值重新绑定你的数据到RepeaterList等数据列表控件。

你可能感兴趣的:(分页)