C# 带偏移量自定义分页方法

本文介绍了一种自定义分页方法的实现细节,该方法通过计算偏移量来确定显示的页码范围,并生成带有高亮显示的页码链接。文章提供了具体的代码示例,包括如何设置每页显示的记录数、当前页数、总记录数以及如何构建页面URL。
    /// <summary>
    /// 带偏移量自定义分页方法
    /// </summary>
    /// <param name="PageSize">每页条数</param>
    /// <param name="CurrentPage">当前页</param>
    /// <param name="TotalCountRecord">总条数</param>
    /// <param name="where">条件</param>
    /// <returns></returns>
    public string BuildPagers(int PageSize, int CurrentPage, int TotalCountRecord,string where)
    {
        //偏移量
        int Step = 5;
        int LeftNum = 0;//左值
        int RightNum = 0;
        string PageUrl = Request.FilePath;
        int PageCount = (int)Math.Ceiling((double)(TotalCountRecord) / PageSize); //总页数 

        //当前页-步长  <1  则作值=1
        if (CurrentPage - Step < 1)
        {
            LeftNum = 1;
        }
        else
        {
            LeftNum = CurrentPage - Step;
        }

        if (CurrentPage + Step > PageCount)
        {
            RightNum = PageCount;
        }
        else
        {
            RightNum = CurrentPage + Step;
        }
        StringBuilder OutPut = new StringBuilder();
        string strUrl = "";
        for (int i = LeftNum; i <= RightNum; i++)
        {
            if (i == CurrentPage)
            {
                OutPut.Append("<font style='margin-left:3px;' color=red>");
                OutPut.Append(i.ToString());
                OutPut.Append("</font>");
            }
            else
            {
                OutPut.Append("<a style='margin-left:3px;' href='");
                OutPut.Append(PageUrl);
                OutPut.Append("?" + where + "&page=");
                OutPut.Append(i.ToString());
                // OutPut.Append(strUrl);
                OutPut.Append("'>");
                OutPut.Append(i.ToString() + " ");
                OutPut.Append("</a>");
            }
        }

        if (CurrentPage > 1)
        {
            OutPut.Insert(0, string.Format("<a href='{0}?" + where + "&page={1}{2}'>上一页</a>", PageUrl, (CurrentPage - 1), strUrl));
        }

        if (CurrentPage < PageCount)
        {
            OutPut.Append("<a href='");
            OutPut.Append(PageUrl);
            OutPut.Append("?" + where + "&page=");
            OutPut.Append(+CurrentPage + 1);
            OutPut.Append(strUrl);
            OutPut.Append("'>下一页</a></li>");
            OutPut.Append("总记录数:<font color='red'>" + TotalCountRecord + "</font>  总页数:<font color='red'>" + PageCount + "</font>");
        }
        return OutPut.ToString();

    }
    private void Bind(int Page,string  where)
    {
        str = BuildPagers(16, Page, GoodsFunBLL.GetGoodsListCount(where), where);
        this.DataGoodsList.DataSource = GoodsFunBLL.GetGoodsByTypeIdList(16, Page,where);
        this.DataGoodsList.DataBind();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值