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

时间:2021-10-11 07:21:10
    /// <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();
    }