實現分頁效果的函數

設置兩個label分別命名爲PageInfo LabCount

  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 (CurrentPage - Step < 1)
        {
            LeftNum = 1;
        }
        else
        {
            LeftNum = CurrentPage - Step;
        }

        if (CurrentPage + Step > PageCount)
        {
            RightNum = PageCount;
        }
        else
        {
            RightNum = CurrentPage + Step;
        }
        StringBuilder OutPut = new StringBuilder();
      
        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("?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}?page={1}{2}'>上一頁</a>", PageUrl, (CurrentPage - 1), strUrl));
        }

        if (CurrentPage < PageCount)
        {
            OutPut.Append("<a href='");
            OutPut.Append(PageUrl);
            OutPut.Append("?page=");
            OutPut.Append(CurrentPage + 1);
            OutPut.Append(strUrl);
            OutPut.Append("'>下一頁</a></li>");
        }
        this.PageInfo.Text = OutPut.ToString();
        this.LabCount.Text = string.Format("總記錄數:<font color='red'>{0}</font>  總頁數:<font color='red'>{1}</font>", TotalCountRecord, PageCount);
    } 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章