Crazy knowledge base

C# 数据分页

<pre language="CSharp">
 #region 数据分页

    public void BuildPagers()
    {
        //偏移量
        int Step = 10;
        int LeftNum = 0;
        int RightNum = 0;
        string PageUrl = Request.FilePath;
        int PageCount = (int)Math.Ceiling((double)(TotalCountRecord) / PageItem);
        if (PageCount <= 1)
            return;
        if (CurrentPage - Step < 1)
        {
            LeftNum = 1;
        }
        else
        {
            LeftNum = CurrentPage - Step;
        }

        if (CurrentPage + Step > PageCount)
        {
            RightNum = PageCount;
        }
        else
        {
            RightNum = CurrentPage + Step;
        }
        StringBuilder OutPut = new StringBuilder();

        if (CurrentPage > 1)
        {
            if (CurrentPage > Step)
            {
                //首页
                OutPut.Append("<a style='margin-left:3px;color:#1E7CBC;' href='");
                OutPut.Append(PageUrl);
                OutPut.Append("?smallid=");
                OutPut.Append(QueryString.GetMenuSmallId);
                OutPut.Append("&postid=");
                OutPut.Append(QueryString.GetPostId);
                OutPut.Append("&page=");
                OutPut.Append(1);
                OutPut.Append(strUrl);
                OutPut.Append("'>|<</a></li>");
            }

            //上一页
            OutPut.Append("<a style='margin-left:3px;color:#1E7CBC;' href='");
            OutPut.Append(PageUrl);
            OutPut.Append("?smallid=");
            OutPut.Append(QueryString.GetMenuSmallId);
            OutPut.Append("&postid=");
            OutPut.Append(QueryString.GetPostId);
            OutPut.Append("&page=");
            OutPut.Append(CurrentPage - 1);
            OutPut.Append(strUrl);
            OutPut.Append("'><<</a></li>");
            //OutPut.Insert(0, string.Format("<a href='{0}?smallid={1}&postid={2}&page={3}{4}'>上一页</a>",QueryString.GetMenuSmallId,QueryString.GetPostId PageUrl, (CurrentPage - 1), strUrl));
        }

        for (int i = LeftNum; i <= RightNum; i++)
        {
            if (i == CurrentPage)
            {
                OutPut.Append("<font style='margin-left:3px;' color=red>");
                OutPut.Append("[");
                OutPut.Append(i.ToString());
                OutPut.Append("]");
                OutPut.Append("</font>");
            }
            else
            {
                OutPut.Append("<a style='margin-left:3px;color:#1E7CBC;'  href='");
                OutPut.Append(PageUrl);
                OutPut.Append("?smallid=");
                OutPut.Append(QueryString.GetMenuSmallId);
                OutPut.Append("&postid=");
                OutPut.Append(QueryString.GetPostId);
                OutPut.Append("&page=");
                OutPut.Append(i.ToString());
                OutPut.Append(strUrl);
                OutPut.Append("'>");
                OutPut.Append("[");
                OutPut.Append(i.ToString());
                OutPut.Append("]");
                OutPut.Append("</a>");
            }
        }

        if (CurrentPage < PageCount)
        {
            //下一页
            OutPut.Append("<a style='margin-left:3px;color:#1E7CBC;' href='");
            OutPut.Append(PageUrl);
            OutPut.Append("?smallid=");
            OutPut.Append(QueryString.GetMenuSmallId);
            OutPut.Append("&postid=");
            OutPut.Append(QueryString.GetPostId);
            OutPut.Append("&page=");
            OutPut.Append(CurrentPage + 1);
            OutPut.Append(strUrl);
            OutPut.Append("'>>></a></li>");

            if ((PageCount - CurrentPage) > Step)
            {
                //尾页
                OutPut.Append("<a style='margin-left:3px;color:#1E7CBC;' href='");
                OutPut.Append(PageUrl);
                OutPut.Append("?smallid=");
                OutPut.Append(QueryString.GetMenuSmallId);
                OutPut.Append("&postid=");
                OutPut.Append(QueryString.GetPostId);
                OutPut.Append("&page=");
                OutPut.Append(TotalCountRecord / PageItem);
                OutPut.Append(strUrl);
                OutPut.Append("'>>|</a></li>");
            }
        }
        this.PageInfo.Text = OutPut.ToString();
        this.PageInfo2.Text = OutPut.ToString();
        //this.LabCount.Text = string.Format("总记录数:<font color='red'>{0}</font>  总页数:<font color='red'>{1}</font>", TotalCountRecord, PageCount);
    }
    #endregion
</pre>
C# 数据分页
滚动到顶部