仿百度、google、Bing分页效果(用于Gridview)

turnPage.ascx.cs:

using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class UserControls_turnPage : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public int TotalNum; // 一共有多少条数据
    public int PageSize; // 每页显示多少条数据
    public string PageName; //转发页面的名称
    public int TotalPage; // 总共页数
    public string PageUrl; //转发路径
    public int PageGroup; //
    public void PageS(int pSize, int tNum, string pName, int pGroup)
    {
        this.PageSize = pSize;
        this.TotalNum = tNum;
        this.PageName = pName;
        this.PageGroup = pGroup;

        if ((TotalNum % PageSize) == 0)
        {
            TotalPage = TotalNum / PageSize;
        }
        else
        {
            TotalPage = TotalNum / PageSize + 1;
        }
        if (PageName.IndexOf("?") > 0)
        {
            PageUrl = PageName + "&";
        }
        else
        {
            PageUrl = PageName + "?";
        }

    }
    public string Installize(int CurPage)
    {
        int pstart = 0; //起始页
        int plast = 0; //终点页

        bool pri = false; //上一页
        bool next = true; //下一页

        int sx = CurPage + 1;
        int sxn = CurPage - 1;

        string strpri = "<a href=" + PageUrl + "Page=" + sxn.ToString() + "><FONT color=#2821A8>上一页</font></a>&nbsp;";
        string strnext = "<a href=" + PageUrl + "Page=" + sx.ToString() + "><FONT color=#2821A8>下一页</font></a>&nbsp;";

        //确定起始页与终点页
        if (CurPage == 1)
        {
            if (TotalPage <= PageGroup)
            {
                pstart = 1;
                plast = TotalPage;
            }
            else
            {
                pstart = 1;
                plast = PageGroup;
            }
        }
        else
        {
            if (CurPage <= PageGroup + 1)
            {
                pstart = 1;
            }
            else
            {
                pstart = CurPage - PageGroup;
            }

            if (CurPage <= TotalPage + 1 - PageGroup)
            {
                plast = CurPage + PageGroup - 1;
            }
            else
            {
                plast = TotalPage;
                next = false;
            }
            pri = true;

        }

        if (pri == false)
        {
            strpri = "";
        }
        if (next == false)
        {
            strnext = "";
        }
        string g = "";
        for (int i = pstart; i <= plast; i++)
        {
            if (CurPage == i)
            {
                g += i.ToString() + "&nbsp;";
            }
            else
            {
                g += "<a href=" + PageUrl + "Page=" + i.ToString() + "><font color=#2821A8>【" + i.ToString() + "】</font></a>&nbsp;";
            }
        }
        return strpri + g + strnext;
    }
}

UsingPage.aspx:

<div style="width:100%; text-align:center">
       <uc1:turnPage ID="turnPage1" runat="server" />
       <asp:Label ID="lbNumber" runat="server"></asp:Label>
        </div>

UsingPage.aspx.cs:

if (Request.QueryString["keyword"] != null)
        {
            keyword = Request.QueryString["keyword"].ToString();
        }
        if (!Page.IsPostBack)
        {
            BindPageData(keyword);
            int numberCount = Int32.Parse(ViewState["numberCount"].ToString());
            if (Request.QueryString["Page"] != null && numberCount > 0)
            {
                int page = Int32.Parse(Request.QueryString["Page"].ToString());
                gvinvite.PageIndex = page - 1;
                BindPageData(keyword);
                this.turnPage1.PageS(4, numberCount, "UsingPage?keyword=" + HttpUtility.UrlEncode(keyword), 5);
                this.lbNumber.Text = this.turnPage1.Installize(Int32.Parse(Request.QueryString["Page"]));
            }
            else
            {
                this.turnPage1.PageS(4, Int32.Parse(ViewState["numberCount"].ToString()), "InvitePostSearch.aspx?keyword=" + HttpUtility.UrlEncode(keyword), 5);
                this.lbNumber.Text = this.turnPage1.Installize(1);
            }

      }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值