建立一个用户控件:Pager.ascx,然后后台代码修改为:
1public partial class Pager : System.Web.UI.UserControl
2...{
3private string _UrlFormat;
4private int _PageSize = 10;
5private int _RecordCount;
6private int _PageCount = 5;
7![]()
8/**//// <summary>
9/// 连接格式
10/// </summary>
11public string UrlFormat
12...{
13get
14...{
15return _UrlFormat;
16}
17set
18...{
19_UrlFormat = value;
20}
21}
22![]()
23/**//// <summary>
24/// 页长度
25![]()
26![]()
27/// </summary>
28public int PageSize
29...{
30get
31...{
32return _PageSize;
33}
34set
35...{
36_PageSize = value;
37}
38}
39![]()
40/**//// <summary>
41/// 当前页码
42/// </summary>
43public int PageIndex
44...{
45get
46...{
47string Pageindex = HttpContext.Current.Request.QueryString["i"];
48if (Pageindex != null)
49...{
50return int.Parse(Pageindex);
51}
52return 1;
53}
54}
55![]()
56/**//// <summary>
57/// 总记录数
58/// </summary>
59public int RecordCount
60...{
61get
62...{
63return _RecordCount;
64}
65set
66...{
67_RecordCount = value;
68}
69}
70![]()
71/**//// <summary>
72/// 两边显示个数
73/// </summary>
74public int PageCount
75...{
76get
77...{
78return _PageCount;
79}
80set
81...{
82_PageCount = value;
83}
84}
85![]()
86protected override void Render(HtmlTextWriter writer)
87...{
88if (RecordCount == 0)
89return;
90int SumPage = (RecordCount + PageSize - 1) / PageSize;
91![]()
92int start = PageIndex - PageCount;
93int end = PageIndex + PageCount;
94![]()
95//以PageIndex为中心,前后个显示Page个页码导航
96![]()
97![]()
98if (SumPage > (PageCount * 2 + 1))
99...{
100if (start < 1)
101...{
102start = 1;
103end = start + 10;
104}
105else if (end > SumPage)
106...{
107start = SumPage - 10;
108end = SumPage;
109}
110}
111else
112...{
113start = 1;
114end = SumPage;
115}
116![]()
117![]()
118![]()
119string tmp = "<a href="" + UrlFormat + "">[{0}]</a>";
120StringBuilder sb = new StringBuilder(string.Format("页次:{0}/{1} 每页:{2} 共计:{3} 条 ", PageIndex, SumPage, PageSize, RecordCount));
121if (PageIndex > 1)
122...{
123sb.Append(string.Format("<a href="" + UrlFormat + "">首页</a> ", 1));
124sb.Append(string.Format(" <a href="" + UrlFormat + "">上一页</a> ", PageIndex - 1));
125}
126for (int i = start; i <= end; i++)
127...{
128if (i == PageIndex)
129...{
130sb.Append(" <strong>" + PageIndex.ToString() + "</strong> ");
131}
132else
133...{
134sb.Append(string.Format(tmp, i));
135}
136sb.Append(" ");
137}
138if (PageIndex < SumPage)
139...{
140sb.Append(string.Format(" <a href="" + UrlFormat + "">下一页</a> ", PageIndex + 1));
141sb.Append(string.Format(" <a href="" + UrlFormat + "">尾页</a>", SumPage));
142}
143writer.Write(sb.ToString());
144}
145protected void Page_Load(object sender, EventArgs e)
146...{
147![]()
148}
149}
使用方法:
把Pager拖拽到页面上,进入页面后台代码,设置如下:
1Pager1.UrlFormat = "?i={0}";//分页格式
2int recordcount, pagecount;
3Repeater1.DataSource = 数据源;
4Repeater1.DataBind();
5Pager1.RecordCount = recordcount;
6




}