当前位置:编程学习 > C#/ASP.NET >>

自己写分页算法

我这里封装在用户控件里。你也可以封装在类文件中。自己定义:
[csharp] 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Text; 
 
namespace HHWeb.UserControl 

    public partial class Paging : System.Web.UI.UserControl 
    { 
        public StringBuilder PagingControlInfo = new StringBuilder(); 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            GetPaging(); 
        } 
        void GetPaging() 
        { 
            //判断如果当前页为负数或者零时,NowPage=1 
            if (NowPage <= 0) 
                NowPage = 1; 
            //判断如果当前页大于总页数时,NowPage=PageCount 
            int PageCount = GetPageCount(); 
            if (NowPage > PageCount) 
                NowPage = PageCount; 
            //首页和上一页() 
            if (NowPage != 1) 
            { 
                PagingControlInfo.Append("<a href=\""+PageName+"?NowPage=1\">首页</a>"); 
                PagingControlInfo.Append("<a href=\""+PageName+"?NowPage=" + (NowPage - 1) + "\">上一页</a>"); 
            } 
            //如果分页总页数小于页码数(PageNumber)时,不显示省略号(...) 
            if (PageCount <= PageNumber) 
            { 
                for (int i = 1; i <= PageCount; i++) 
                    GetPageNumberStr(i); 
            } 
            else 
            { 
                if (PageNumber % 2 != 0) 
                { 
                    LeftPageNumberOne = (PageNumber / 2) + 1; 
                    LeftPageNumberTwo = (PageNumber / 2); 
                    RightPageNumber = (PageNumber / 2); 
                } 
                else 
                { 
                    LeftPageNumberOne = (PageNumber / 2) + 1; 
                    LeftPageNumberTwo = (PageNumber / 2); 
                    RightPageNumber = (PageNumber / 2) - 1; 
                } 
                //总页数大于页码数(PageNumber)时(简单处理--算法) 
                if (NowPage > LeftPageNumberOne && NowPage < PageCount - RightPageNumber) 
                { 
                    PagingControlInfo.Append("<span>...</span>"); 
                    for (int i = NowPage - LeftPageNumberTwo; i <= NowPage + RightPageNumber; i++) 
                        GetPageNumberStr(i); 
                    PagingControlInfo.Append("<span>...</span>"); 
                } 
                else if (NowPage >= PageCount - RightPageNumber) 
                { 
                    PagingControlInfo.Append("<span>...</span>"); 
                    for (int i = (PageCount - PageNumber) + 1; i <= PageCount; i++) 
                        GetPageNumberStr(i); 
                } 
                else 
                { 
                    for (int i = 1; i <= PageNumber; i++) 
                     &nb

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,