ASP.NET 分页导航条

本文介绍了一种ASP.NET中实现分页导航的方法。通过分析当前请求URL来构造分页链接,支持动态调整显示的页码数量,并且可以跳转到首页、上一页、下一页及尾页。
protected static string Paging(int pageIndex, int pageCount)
{
    string url = HttpContext.Current.Request.RawUrl;            
    if (url.IndexOf("?") != -1)
    {
        if (url.LastIndexOf("?page") != -1)
        {
            url = url.Substring(0, url.LastIndexOf('?'));
            url += "?";
        }
        else
            url += "&";
    }
    else
    {
        url += "?";
    }
 
    pageIndex = TypeConverter.StrToInt(HttpContext.Current.Request.QueryString["page"].ToString());
                 
    int start = 1;
    int end = 5;
    int prev = 0;
    int next = 0;
    StringBuilder html = new StringBuilder();
 
    if (pageIndex < 1) //页码
    {
        pageIndex = 1;
    }
    if (pageIndex > pageCount) //总页数
    {
        pageIndex = pageCount;
    }
 
    next = pageIndex + 1;
    prev = pageIndex - 1;
 
    if (pageIndex > 3)
    {
        start = pageIndex - 2;
        end = pageIndex + 2;
    }
 
    if (start < 1)
    {
        start = 1;
    } //为了避免输出的时候产生负数,设置如果小于就从序号开始
 
    if (pageCount < end)
    {
        end = pageCount;
    }
 
    if (pageIndex > 1)
    {
        html.AppendLine("<a href=\"" + url + "page=1\">首页</a>");
        html.AppendLine("<a href=\"" + url + "page=" + prev + "\">上一页</a>");
    }
 
    for (int i = start; i <= end; i++)
    {
        if (pageIndex == i)
            html.AppendLine("<span><a href=\"" + url + "page=" + i.ToString() + "\">" + i.ToString() + "</a></span>");
        else
            html.AppendLine("<a href=\"" + url + "page=" + i.ToString() + "\">" + i.ToString() + "</a>");
    }
 
    if (pageIndex != pageCount)
    {
        html.AppendLine("<a href=\"" + url + "page=" + next + "\">下一页</a>");
        html.AppendLine("<a href=\"" + url + "page=" + pageCount + "\">尾页</a>");
    }
 
    return html.ToString();
}

 

转载于:https://www.cnblogs.com/bestfriends/archive/2013/03/25/aspnet-paging-navigation.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值