小清新流打造Repeater&DataList分页

借用cb君的劳动成果哈~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
 
/// <summary>
///PageHelper 的摘要说明
/// </summary>
public class PageHelper
{
	public PageHelper()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}
 
    /// <summary>
    /// 绑定分页到DataList
    /// </summary>
    /// <param name="ds">数据集</param>
    /// <param name="dl">DataList控件</param>
    /// <param name="strFilePath">当前文件路径</param>
    /// <param name="lnkPrev">上一页</param>
    /// <param name="lnkCurrent">当前页</param>
    /// <param name="lnkNext">下一页</param>
    /// <param name="iPageSize">每页显示数据条数</param>
    /// <param name="iPage">请求页</param>
    /// <returns></returns>
 
    public static int DataListPaging(DataSet ds, DataList dl,string strFilePath, HyperLink lnkPrev, HyperLink lnkCurrent, HyperLink lnkNext, int iPageSize, int iPage)
    {
        PagedDataSource objPD = Paging(ds, strFilePath, lnkPrev, lnkCurrent, lnkNext, iPageSize, iPage);
        if (null != objPD)
        {
            dl.DataSource = objPD;
            dl.DataBind();
            return objPD.PageCount;
        }
        else
            return 0;
    }
 
    /// <summary>
    /// 绑定数据到repeater
    /// </summary>
    /// <param name="ds">数据集</param>
    /// <param name="re">repeater控件</param>
    /// <param name="strFilePath">文件路径</param>
    /// <param name="lnkPrev">上一页</param>
    /// <param name="lnkCurrent">当前页</param>
    /// <param name="lnkNext">下一页</param>
    /// <param name="iPageSize">每页显示数据条数</param>
    /// <param name="iPage">请求页</param>
    /// <returns></returns>
    public static int RepeaterPaging(DataSet ds, Repeater re, string strFilePath, HyperLink lnkPrev, HyperLink lnkCurrent, HyperLink lnkNext, int iPageSize, int iPage)
    {
        PagedDataSource objPD = Paging(ds, strFilePath, lnkPrev, lnkCurrent, lnkNext, iPageSize, iPage);
        if (null != objPD)
        {
            re.DataSource = objPD;
            re.DataBind();
            return objPD.PageCount;
        }
        else
            return 0;
    }
 
    /// <summary>
    /// 分页绑定
    /// </summary>
    /// <param name="ds">数据集</param>
    /// <param name="strFilePath">当前文件名</param>
    /// <param name="rControl">绑定的Repeater控件</param>
    /// <param name="lnkPrev">上一页链接</param>
    /// <param name="lnkCurrent">当前页链接</param>
    /// <param name="lnkNext">下一页链接</param>
    /// <param name="iPageSize">每页显示的数据</param>
    /// <param name="iPage">请求的页面</param>
    /// <returns>总页数</returns>
    public static PagedDataSource Paging(DataSet ds,string strFilePath,HyperLink lnkPrev,HyperLink lnkCurrent,HyperLink lnkNext,int iPageSize,int iPage)
    {
        //判断ds中是否有数据
        if (ds.Tables[0].Rows.Count == 0)
        {
            lnkPrev.Visible = false;
            lnkNext.Visible = false;   //设置超链接为隐藏
            return null;
        }
        //对PagedDataSource 对象的相关属性赋值 
        PagedDataSource objPds = new PagedDataSource();
        objPds.DataSource = ds.Tables[0].DefaultView;
        objPds.AllowPaging = true;
        objPds.PageSize = iPageSize;
        int totalPage = objPds.PageCount;   //总页数
        int CurrentPage;             //要显示的页面
        int requirePage = iPage;    //请求页
        //当前页面从Page查询参数获取 并判断是否有效
        if (requirePage > 0 && requirePage <= totalPage)
            CurrentPage = iPage;
        else
            CurrentPage = 1;
 
        objPds.CurrentPageIndex = CurrentPage - 1;
        lnkCurrent.Text = "第" + CurrentPage.ToString() + "页";
 
        if (!objPds.IsFirstPage)
            lnkPrev.NavigateUrl = strFilePath + "?Page=" + Convert.ToString(CurrentPage - 1);
        else
            lnkPrev.Visible = false;
 
        if (!objPds.IsLastPage)
            lnkNext.NavigateUrl = strFilePath + "?Page=" + Convert.ToString(CurrentPage + 1);
        else
            lnkNext.Visible = false;
 
        return objPds;  //返回PageDataSource对象
    }
}