ASP.NET中Repeater控件和DataList控件通用的分页类

本文利用PagedDataSource+Repeater控件实现数字分页,

DataList控件也可以通过修改ID名称实现分页。

/// <summary>
/// Repeater分页
/// </summary>
/// <param name="ds">DataSet实例</param>
/// <param name="RepeaterName">Repeater ID名称</param>
/// <param name="pagesize">分页大小</param>
/// <returns></returns>

public static string GetRptPageNum(DataSet ds, Repeater RepeaterName, int pagesize)

{

            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = ds.Tables[0].DefaultView;
            pds.AllowPaging = true;
            //得到数据总行数
            int total = ds.Tables[0].Rows.Count;
            pds.PageSize = pagesize;
            //定义索引页
            int page;
            if (HttpContext.Current.Request.QueryString["page"] != null)
            {
                page = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]);
            }
            else
            {
                page = 1;
            }
            //索引从0开始算起,所以-1
            pds.CurrentPageIndex = page - 1;
            RepeaterName.DataSource = pds;
            RepeaterName.DataBind();
            int allpage = 0;
            int next = 0;
            int pre = 0;
            int startcount = 0;
            int endcount = 0;
            string pagestr = "";
            if (page < 1)
            {
                page = 1;
            }
            //计算总页数
            if (pagesize != 0)
            {
                allpage = (total / pagesize); //总数量除于每页显示量
                allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage);
                allpage = (allpage == 0 ? 1 : allpage);
            }
            next = page + 1;
            pre = page - 1;
            //中间页起始序号
            startcount = (page + 5) > allpage ? allpage - 9 : page - 4;
            //中间页终止序号
            endcount = page < 5 ? 10 : page + 5;
            //设置序号从1开始
            if (startcount < 1)
            {
                startcount = 1;
            }
            //控制页码数在总页码数内
            if (allpage < endcount)
            {
                endcount = allpage;
            }
            pageurl= "共" + allpage + "页"+ total +"条"+"&nbsp;&nbsp;";
            pageurl+= page > 1 ? "<a href  = \"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page = 1\">首页</a>&nbsp;&nbsp;<a href = \"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page = " + pre + "\"> 上一页</a>" : "<a>首页</a>" + "&nbsp;&nbsp;" + "<a>上一页</a>";
            for (int i = startcount; i <= endcount; i++)
            {
                pageurl+= page == i ? "&nbsp;&nbsp;" + "<a class=\"cpb\">" + i + "</a>" : "&nbsp;&nbsp;<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + i + "\">" + i + "</a>";
            }
            pageurl+= page != allpage ? "&nbsp;&nbsp;<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + next + "\">下一页</a>&nbsp;&nbsp;<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + allpage + "\">末页</a>" : "&nbsp;&nbsp;" + "<a >下一页</a>" + "&nbsp;&nbsp;" + "<a >末页</a>";
            return pageurl;
    

}

其他分页需求及美化可以根据自己的需要去改,如下效果图!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值