.Net通用分页类(存储过程分页版,完全自定义显示样式,包括中英显示)

using  System;
using  System.Collections.Generic;
using  System.Text;

ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
/// .Net通用分页类(存储过程分页版,完全自定义显示样式,包括中英显示)
/// 作者:启程 www.letwego.cn
/// 可用于任意用途,请保留作者信息,谢谢!
/// </summary>

namespace  Ask.Common
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 分页(只处理分页,与数据无关)
    
/// </summary>

    public class PageStyle
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
初始构造#region 初始构造
        
public PageStyle(string Language)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (Language == "EN")//英文默认
ExpandedSubBlockStart.gifContractedSubBlock.gif
            {
                PageString 
= "Page&nbsp;[PageIndex]/[TotalPage]&nbsp;&nbsp;Info&nbsp;[PageSize]/[TotalRecord]&nbsp;&nbsp;[FirstStr]&nbsp;[PrevStr]&nbsp;[PageNumber]&nbsp;[NextStr]&nbsp;[LastStr]&nbsp;&nbsp;[TurnControl]";
                TurnUrlStr 
= "";
                PageSize 
= 20;
                PageIndex 
= 1;
                TotalRecord 
= 0;
                FirstStr 
= "<<";
                PrevStr 
= "<";
                NextStr 
= ">";
                LastStr 
= ">>";
                NoRecord 
= "Total 0";
                ButtonStr 
= " GO ";

                TotalPage 
= 0;
                TurnControl 
= "";
                PageNumber 
= "";
                ShowPageStr 
= "";
            }

            
else//中文默认
ExpandedSubBlockStart.gifContractedSubBlock.gif
            {
                PageString 
= "第[PageIndex]页/共[TotalPage]页&nbsp;&nbsp;第每[PageSize]条/共[TotalRecord]条&nbsp;&nbsp;[FirstStr]&nbsp;[PrevStr]&nbsp;[PageNumber]&nbsp;[NextStr]&nbsp;[LastStr]&nbsp;&nbsp;[TurnControl]";
                TurnUrlStr 
= "";
                PageSize 
= 20;
                PageIndex 
= 1;
                TotalRecord 
= 0;
                FirstStr 
= "首页";
                PrevStr 
= "上一页";
                NextStr 
= "下一页";
                LastStr 
= "尾页";
                NoRecord 
= "共有 0 条信息";
                ButtonStr 
= "跳转";

                TotalPage 
= 0;
                TurnControl 
= "";
                PageNumber 
= "";
                ShowPageStr 
= "";
            }

        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
字段 公共#region 字段 公共
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 具体样式(需要显示哪些内容)
        
/// </summary>

        public string PageString;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 跳转 的url链接
        
/// </summary>

        public string TurnUrlStr;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 跳转的url链接的参数前面不要加问号和与号
        
/// </summary>

        public string Parameters;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 每页记录数
        
/// </summary>

        public int PageSize;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 需要获取第几页的数据,从 1 开始
        
/// </summary>

        public int PageIndex;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 总记录数
        
/// </summary>

        public int TotalRecord;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 首页 显示样式
        
/// </summary>

        public string FirstStr;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 上一页 显示样式
        
/// </summary>

        public string PrevStr;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 下一页 显示样式
        
/// </summary>

        public string NextStr;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 尾页 显示样式
        
/// </summary>

        public string LastStr;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 没有记录时显示的信息
        
/// </summary>

        public string NoRecord;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 跳转按钮文字
        
/// </summary>

        public string ButtonStr;
        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
字段 私有#region 字段 私有
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 总页数
        
/// </summary>

        private int TotalPage;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 跳转控件
        
/// </summary>

        private string TurnControl;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 存储中间的内容(如 1 2 3 页码)
        
/// </summary>

        private string PageNumber;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 显示页码的总字符
        
/// </summary>

        private string ShowPageStr;

        
#endregion



ContractedSubBlock.gifExpandedSubBlockStart.gif        
获取最终页码显示#region 获取最终页码显示
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 获取最终页码显示
        
/// </summary>
        
/// <param name="Style">样式(1 是上下页,2 是显示一批页码)</param>
        
/// <param name="PlaceIn">固定当前页在第几个位置(样式2用)</param>
        
/// <param name="ShowNum">一页显示几个页码(样式2用)</param>
        
/// <returns>最终页码显示</returns>

        public string GetShowPageStr(string Style, int PlaceIn, int ShowNum)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            TotalPage 
= (TotalRecord + PageSize - 1/ PageSize;

            
//超出最小页码
            if (PageIndex < 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                PageIndex 
= 1;
            }


            
//超出最大页码
            if (PageIndex > TotalPage)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                PageIndex 
= TotalPage;
            }


            
//跳转
            TurnControl = "<input value='" + PageIndex.ToString() + "' id='txtPageGo' name='txtPageGo' type='text' style='width:35px;' οnkeydοwn=\"if(event.keyCode==13)window.location.href='" + TurnUrlStr + "?Page=' + document.getElementById('txtPageGo').value + '" + "&" + Parameters + "'\"><input name='btnGo' type='button' id='btnGo' value='" + ButtonStr + "' οnclick=\"javascript:window.location.href='" + TurnUrlStr + "?Page=' + document.getElementById('txtPageGo').value + '" + "&" + Parameters + "'\">";

            
//没有记录
            if (TotalRecord <= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                ShowPageStr 
= NoRecord;
            }

            
//有记录
            else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//只有一页
                if (TotalPage <= 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    ShowPageStr 
= "共有 " + TotalRecord.ToString() + " 条信息";
                }

                
//不止一页
                else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
//第一页
                    if (PageIndex == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                    }

                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        FirstStr 
= "<a href=\"" + TurnUrlStr + "?Page=1" + "&" + Parameters + "\">" + FirstStr + "</a>";
                        PrevStr 
= "<a href=\"" + TurnUrlStr + "?Page=" + Convert.ToString(PageIndex - 1) + "&" + Parameters + "\">" + PrevStr + "</a>";
                    }


                    
//最后一页
                    if (PageIndex == TotalPage)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                    }

                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        NextStr 
= "<a href=\"" + TurnUrlStr + "?Page=" + Convert.ToString(PageIndex + 1) + "&" + Parameters + "\">" + NextStr + "</a>";
                        LastStr 
= "<a href=\"" + TurnUrlStr + "?Page=" + TotalPage + "&" + Parameters + "\">" + LastStr + "</a>";
                    }

                }



ContractedSubBlock.gifExpandedSubBlockStart.gif                
样式一: 共X条信息 第N页/共M页 首页 上一页 下一页 尾页  跳转#region 样式一: 共X条信息 第N页/共M页 首页 上一页 下一页 尾页  跳转
                
if (Style == "1")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    PageNumber 
= "";
                }

                
#endregion



ContractedSubBlock.gifExpandedSubBlockStart.gif                
样式三: 共X条信息 第N页/共M页 首页 1 2 3 尾页 跳转 当前页码定位在第 PlaceIn 位#region 样式三: 共X条信息 第N页/共M页 首页 1 2 3 尾页 跳转 当前页码定位在第 PlaceIn 位
                
if (Style == "2")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
int PageTemp = 0;
                    
if (PlaceIn < 1 || PlaceIn > ShowNum)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        PlaceIn 
= 2;//前面保持有两个,则当前页码定位在第3位
                    }

                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        PlaceIn 
= PlaceIn - 1;//前面保持有 PlaceIn - 1 个
                    }

                    
string strPageNum = "";
                    
string strTempNow = "";

                    
//当页码总数 <= 一页要显示的页码数
                    if (TotalPage <= ShowNum)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
for (int i = 1; i <= TotalPage; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            strTempNow 
= Convert.ToString(PageTemp + i);

                            
//当前页不显示超链接
                            if (PageIndex == PageTemp + i)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
{
                                strPageNum 
= strPageNum + "<b>" + strTempNow + "</b>&nbsp;&nbsp;";
                            }

                            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
{
                                strPageNum 
= strPageNum + "<a href=\"" + TurnUrlStr + "?Page=" + strTempNow + "&" + Parameters + "\">[" + strTempNow + "]</a>&nbsp;&nbsp;";
                            }

                        }

                    }

                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
//当前页码 <= 固定位置 PlaceIn
                        if (PageIndex <= PlaceIn)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            
for (int i = 1; i <= ShowNum; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
{
                                strTempNow 
= Convert.ToString(i);

                                
//当前页不显示超链接
                                if (PageIndex == i)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
{
                                    strPageNum 
= strPageNum + "<b>" + strTempNow + "</b>&nbsp;&nbsp;";
                                }

                                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
{
                                    strPageNum 
= strPageNum + "<a href=\"" + TurnUrlStr + "?Page=" + strTempNow + "&" + Parameters + "\">[" + strTempNow + "]</a>&nbsp;&nbsp;";
                                }

                            }

                        }

                        
//当前页码 >=最后一批该显示
                        else if (PageIndex >= TotalPage - ShowNum + PlaceIn + 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            
//第一个显示的数字
                            PageTemp = TotalPage - ShowNum + 1;
                            
for (int i = PageTemp; i <= TotalPage; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
{
                                
if (i > TotalPage) break;

                                strTempNow 
= Convert.ToString(i);

                                
//当前页不显示超链接
                                if (PageIndex == i)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
{
                                    strPageNum 
= strPageNum + "<b>" + strTempNow + "</b>&nbsp;&nbsp;";
                                }

                                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
{
                                    strPageNum 
= strPageNum + "<a href=\"" + TurnUrlStr + "?Page=" + strTempNow + "&" + Parameters + "\">[" + strTempNow + "]</a>&nbsp;&nbsp;";
                                }

                            }

                        }

                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            
for (int i = PageIndex - PlaceIn; i < PageIndex - PlaceIn + ShowNum; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
{
                                strTempNow 
= Convert.ToString(i);

                                
//当前页不显示超链接
                                if (i == PageIndex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
{
                                    strPageNum 
= strPageNum + "<b>" + strTempNow + "</b>&nbsp;&nbsp;";
                                }

                                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
{
                                    strPageNum 
= strPageNum + "<a href=\"" + TurnUrlStr + "?Page=" + strTempNow + "&" + Parameters + "\">[" + strTempNow + "]</a>&nbsp;&nbsp;";
                                }

                            }

                        }

                    }




                    PageNumber 
= strPageNum;
                }

                
#endregion

            }


            ShowPageStr 
= PageString;

            ShowPageStr 
= ShowPageStr.Replace("[PageIndex]", PageIndex.ToString());//
            ShowPageStr = ShowPageStr.Replace("[TotalPage]", TotalPage.ToString());//
            ShowPageStr = ShowPageStr.Replace("[PageSize]", PageSize.ToString());//
            ShowPageStr = ShowPageStr.Replace("[TotalRecord]", TotalRecord.ToString());//
            ShowPageStr = ShowPageStr.Replace("[FirstStr]", FirstStr);//
            ShowPageStr = ShowPageStr.Replace("[PrevStr]", PrevStr);//
            ShowPageStr = ShowPageStr.Replace("[NextStr]", NextStr);//
            ShowPageStr = ShowPageStr.Replace("[LastStr]", LastStr);//
            ShowPageStr = ShowPageStr.Replace("[TurnControl]", TurnControl);//
            ShowPageStr = ShowPageStr.Replace("[PageNumber]", PageNumber);//

            
return ShowPageStr;
        }

        
#endregion

    }


ContractedSubBlock.gifExpandedSubBlockStart.gif    
调用实例#region 调用实例
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
    protected void BindData()
    {
        PageStyle MyPage = new PageStyle("EN");//创建类
        MyPage.PageSize = MyPageSize;//页码大小
        MyPage.TotalRecord = Counts;//总记录数
        MyPage.TurnUrlStr = "AskSearchList.aspx";//要跳转的页面(当前页)
        MyPage.Parameters = MyParameters;//页面所带参数
        MyPage.PageIndex = PageIndex;//当前要显示的页码
        //具体样式
        MyPage.PageString = "第[PageIndex]页/共[TotalPage]页&nbsp;&nbsp;第每[PageSize]条/共[TotalRecord]条&nbsp;&nbsp;[FirstStr]&nbsp;[PrevStr]&nbsp;[PageNumber]&nbsp;[NextStr]&nbsp;[LastStr]&nbsp;&nbsp;[TurnControl]";

        string strPage = MyPage.GetShowPageStr("2", 3, 10);
        labShowPage.Text = strPage;
    }
    
*/

    
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif    
简要说明#region 简要说明
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
    .Net通用分页类(存储过程分页版,可以完全自定义显示样式,包括中英显示).
    大概思路是:主要是利用存储过程在数据库进行分页,
    所以在这个类里面不涉及到数据的处理,只进行页码的显示格式处理,
    配合SQL2005 的 ROW_NUMBER () 功能,能够达到更好的效果.
    显示样式可以完全自定义,,用图片也可以,只是要把字符串拼成图片的Html
    效果图:
https://images.cnblogs.com/cnblogs_com/84ww/128905/r_PageStore.gif
    
*/

    
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif    
附 存储过程例子#region 附 存储过程例子
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
    CREATE PROCEDURE dbo.sp_userinfoList

        @strOptions varchar(200) = NULL,
        @PageSize int = 20,
        @PageIndex int = 1,
        @Counts int = 0 OUTPUT

    AS

    SET NOCOUNT ON
    SET ANSI_WARNINGS OFF


    DECLARE @PageUp int
    DECLARE @PageDown int    
    
    --处理页大小
    IF @PageSize = -1 SET @PageSize = 20

    --获得总记录数
    SELECT @Counts = COUNT(1) FROM userinfo

    --当前页的第一条记录RowID
    SET @PageDown = @PageSize * @PageIndex + 1

    --当前页的最后一条记录RowID
    SET @PageUp = @PageSize * (@PageIndex + 1)


    --  信息列表  
    IF @strOptions='LIST' BEGIN

        SELECT
            id,
            user,
            password,
            datetime,
            content
        FROM

        (
            SELECT * ,
            PageTableRowID = ROW_NUMBER () OVER (ORDER BY id DESC)
            FROM userinfo
        ) AS PageTableList
        WHERE PageTableRowID BETWEEN @PageDown AND @PageUp 
    END
    
*/

    
#endregion

}



转载于:https://www.cnblogs.com/84ww/archive/2008/08/02/1258744.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值