通用手动分页方法
特点:
多参数传递,适合多种数据绑定控件,便于手写干净的代码
函数代码如下:
代码
1 /// <summary>
2 /// 分页链接生成函数,支持多参数传递 Code By Shaoyun
3 /// </summary>
4 /// <param name="nRecords">记录总数</param>
5 /// <param name="nPageSize">分页尺寸</param>
6 /// <param name="nShowNum">显示的分页链接数</param>
7 /// <param name="paramName">页面传递的链接参数名称</param>
8 /// <returns></returns>
9 public static string GetPageLinks(int nRecords, int nPageSize, int nShowNum, string paramName)
10 {
11 paramName = IsN(paramName) ? "page" : paramName;
12 string sHtml =
13 "<span class=\"info\">共{0}条</span> <span class=\"info\">{1}条/页</span> <span class=\"info\">第{2}/{3}页 </span> ";
14 string sUrl = HttpContext.Current.Request.CurrentExecutionFilePath;
15 ArrayList sParam= new ArrayList();
16
17 // 获取前导URL
18 System.Collections.Specialized.NameValueCollection collParams = new NameValueCollection(HttpContext.Current.Request.QueryString);
19 if (!IsN(collParams[paramName]))
20 {
21 collParams.Remove(paramName);
22 }
23 if (collParams.Count>0)
24 {
25 foreach (string item in collParams.AllKeys)
26 {
27 sParam.Add(string.Format("{0}={1}", item, collParams[item]));
28 }
29 sUrl += string.Format("?{0}&{1}=", String.Join("&", (string[]) sParam.ToArray(typeof (string))), paramName);
30 }
31 else
32 {
33 sUrl += string.Format("?{0}=", paramName);
34 }
35
36 int nPage = 0, nFirst = 1, nLast = 0, nPrev = 0, nNext = 0, nCur= 0;
37
38 nPage = nRecords%nPageSize == 0 ? nRecords/nPageSize : (nRecords/nPageSize) + 1;
39
40 nPage = nPage == 0 ? 1 : nPage;
41 nLast = nPage;
42 // 处理当前页
43 nCur = ToInt(HttpContext.Current.Request.QueryString[paramName]);
44 nCur = nCur == 0 ? 1 : (nCur > nPage ? nPage : nCur);
45
46 nPrev = nCur - 1;
47 nNext = nCur + 1;
48
49 StringBuilder sb = new StringBuilder();
50 sb.Append(string.Format("<a href=\"{0}\">首页</a> ", sUrl + nFirst.ToString()));
51 if (nPrev>1)
52 {
53 sb.Append(string.Format("<a href=\"{0}\">上一页</a> ", sUrl + nPrev.ToString()));
54 }
55
56 int fbegin = 0, fend = 0, nShowFront = 0, nShowBack = 0;
57 if(nPage<nShowNum)
58 {
59 fbegin = 1;
60 fend = nPage;
61 }
62 else
63 {
64 // 当前页前后显示的页面数量
65 nShowFront = Convert.ToInt32(nShowNum/2);
66 nShowBack = Convert.ToInt32(nShowNum/2);
67 // 要显示页码数为偶数时,确定前后显示的页码数量
68 if(nShowNum % 2 ==0)
69 {
70 nShowFront--;
71 }
72 if(nCur-nShowFront>0)
73 {
74 fbegin = nCur-nShowFront;
75 }
76 else
77 {
78 fbegin = 1;
79 }
80 fend = nCur + nShowBack;
81
82 // 修正结束页码小于要显示的页面数
83 if(fend<nShowNum)
84 {
85 fend = nShowNum;
86 }
87 // 修正结束页码,如果超出总页数
88 if (fend > nPage)
89 {
90 fend = nPage;
91 }
92 // 修正开始页码小于要显示的页面数,比如总10页显示5个链接当前第10页,开始页计算的为8,只会显示9-10页链接,起始页应为6
93 if(fend-fbegin + 1<nShowNum)
94 {
95 fbegin = fend - nShowNum + 1;
96 }
97 // 再次修正一下起始页,必须大于0
98 fbegin = fbegin < 1 ? 1 : fbegin;
99 }
100 for (int i = fbegin; i < fend +1; i++)
101 {
102 if (i==nCur)
103 {
104 sb.Append(string.Format("<span class=\"current\">{0}</span> ", nCur.ToString()));
105 }
106 else
107 {
108 sb.Append(string.Format("<a href=\"{0}\">{1}</a> ", sUrl + i.ToString(), i.ToString()));
109 }
110 }
111 if (nCur < nLast)
112 {
113 sb.Append(string.Format("<a href=\"{0}\">下一页</a> ", sUrl + nNext.ToString()));
114 }
115 sb.Append(string.Format("<a href=\"{0}\">尾页</a>", sUrl + nLast.ToString()));
116
117 return string.Format(sHtml, nRecords, nPageSize, nCur, nPage)+ sb.ToString();
118 }
119 /// <summary>
120 /// 重载的分页链接生成函数,减少一个参数
121 /// </summary>
122 /// <param name="nRecords"></param>
123 /// <param name="nPageSize"></param>
124 /// <param name="nShowNum"></param>
125 /// <returns></returns>
126 public static string GetPageLinks(int nRecords, int nPageSize, int nShowNum)
127 {
128 return GetPageLinks(nRecords, nPageSize, nShowNum, "page");
129 }
2 /// 分页链接生成函数,支持多参数传递 Code By Shaoyun
3 /// </summary>
4 /// <param name="nRecords">记录总数</param>
5 /// <param name="nPageSize">分页尺寸</param>
6 /// <param name="nShowNum">显示的分页链接数</param>
7 /// <param name="paramName">页面传递的链接参数名称</param>
8 /// <returns></returns>
9 public static string GetPageLinks(int nRecords, int nPageSize, int nShowNum, string paramName)
10 {
11 paramName = IsN(paramName) ? "page" : paramName;
12 string sHtml =
13 "<span class=\"info\">共{0}条</span> <span class=\"info\">{1}条/页</span> <span class=\"info\">第{2}/{3}页 </span> ";
14 string sUrl = HttpContext.Current.Request.CurrentExecutionFilePath;
15 ArrayList sParam= new ArrayList();
16
17 // 获取前导URL
18 System.Collections.Specialized.NameValueCollection collParams = new NameValueCollection(HttpContext.Current.Request.QueryString);
19 if (!IsN(collParams[paramName]))
20 {
21 collParams.Remove(paramName);
22 }
23 if (collParams.Count>0)
24 {
25 foreach (string item in collParams.AllKeys)
26 {
27 sParam.Add(string.Format("{0}={1}", item, collParams[item]));
28 }
29 sUrl += string.Format("?{0}&{1}=", String.Join("&", (string[]) sParam.ToArray(typeof (string))), paramName);
30 }
31 else
32 {
33 sUrl += string.Format("?{0}=", paramName);
34 }
35
36 int nPage = 0, nFirst = 1, nLast = 0, nPrev = 0, nNext = 0, nCur= 0;
37
38 nPage = nRecords%nPageSize == 0 ? nRecords/nPageSize : (nRecords/nPageSize) + 1;
39
40 nPage = nPage == 0 ? 1 : nPage;
41 nLast = nPage;
42 // 处理当前页
43 nCur = ToInt(HttpContext.Current.Request.QueryString[paramName]);
44 nCur = nCur == 0 ? 1 : (nCur > nPage ? nPage : nCur);
45
46 nPrev = nCur - 1;
47 nNext = nCur + 1;
48
49 StringBuilder sb = new StringBuilder();
50 sb.Append(string.Format("<a href=\"{0}\">首页</a> ", sUrl + nFirst.ToString()));
51 if (nPrev>1)
52 {
53 sb.Append(string.Format("<a href=\"{0}\">上一页</a> ", sUrl + nPrev.ToString()));
54 }
55
56 int fbegin = 0, fend = 0, nShowFront = 0, nShowBack = 0;
57 if(nPage<nShowNum)
58 {
59 fbegin = 1;
60 fend = nPage;
61 }
62 else
63 {
64 // 当前页前后显示的页面数量
65 nShowFront = Convert.ToInt32(nShowNum/2);
66 nShowBack = Convert.ToInt32(nShowNum/2);
67 // 要显示页码数为偶数时,确定前后显示的页码数量
68 if(nShowNum % 2 ==0)
69 {
70 nShowFront--;
71 }
72 if(nCur-nShowFront>0)
73 {
74 fbegin = nCur-nShowFront;
75 }
76 else
77 {
78 fbegin = 1;
79 }
80 fend = nCur + nShowBack;
81
82 // 修正结束页码小于要显示的页面数
83 if(fend<nShowNum)
84 {
85 fend = nShowNum;
86 }
87 // 修正结束页码,如果超出总页数
88 if (fend > nPage)
89 {
90 fend = nPage;
91 }
92 // 修正开始页码小于要显示的页面数,比如总10页显示5个链接当前第10页,开始页计算的为8,只会显示9-10页链接,起始页应为6
93 if(fend-fbegin + 1<nShowNum)
94 {
95 fbegin = fend - nShowNum + 1;
96 }
97 // 再次修正一下起始页,必须大于0
98 fbegin = fbegin < 1 ? 1 : fbegin;
99 }
100 for (int i = fbegin; i < fend +1; i++)
101 {
102 if (i==nCur)
103 {
104 sb.Append(string.Format("<span class=\"current\">{0}</span> ", nCur.ToString()));
105 }
106 else
107 {
108 sb.Append(string.Format("<a href=\"{0}\">{1}</a> ", sUrl + i.ToString(), i.ToString()));
109 }
110 }
111 if (nCur < nLast)
112 {
113 sb.Append(string.Format("<a href=\"{0}\">下一页</a> ", sUrl + nNext.ToString()));
114 }
115 sb.Append(string.Format("<a href=\"{0}\">尾页</a>", sUrl + nLast.ToString()));
116
117 return string.Format(sHtml, nRecords, nPageSize, nCur, nPage)+ sb.ToString();
118 }
119 /// <summary>
120 /// 重载的分页链接生成函数,减少一个参数
121 /// </summary>
122 /// <param name="nRecords"></param>
123 /// <param name="nPageSize"></param>
124 /// <param name="nShowNum"></param>
125 /// <returns></returns>
126 public static string GetPageLinks(int nRecords, int nPageSize, int nShowNum)
127 {
128 return GetPageLinks(nRecords, nPageSize, nShowNum, "page");
129 }
用到的相关函数:
代码
1 /// <summary>
2 /// 转换任意类型为Int,默认值为0
3 /// </summary>
4 /// <param name="expr"></param>
5 /// <returns></returns>
6
7 public static int ToInt(object expr)
8 {
9 return expr != null ? ToInt(expr.ToString()) : 0;
10 }
11
12 public static int ToInt(string str)
13 {
14 return IsNumeric(str) ? Convert.ToInt32(str) : 0;
15 }
16
17 /// <summary>
18 /// 判断对象是否为Int32类型的数字
19 /// </summary>
20 /// <param name="Expression"></param>
21 /// <returns></returns>
22 public static bool IsNumeric(object expression)
23 {
24 if (expression != null)
25 return IsNumeric(expression.ToString());
26
27 return false;
28
29 }
30
31 /// <summary>
32 /// 判断对象是否为Int32类型的数字
33 /// </summary>
34 /// <param name="Expression"></param>
35 /// <returns></returns>
36 public static bool IsNumeric(string expression)
37 {
38 if (expression != null)
39 {
40 string str = expression;
41 if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
42 {
43 if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
44 return true;
45 }
46 }
47 return false;
48 }
2 /// 转换任意类型为Int,默认值为0
3 /// </summary>
4 /// <param name="expr"></param>
5 /// <returns></returns>
6
7 public static int ToInt(object expr)
8 {
9 return expr != null ? ToInt(expr.ToString()) : 0;
10 }
11
12 public static int ToInt(string str)
13 {
14 return IsNumeric(str) ? Convert.ToInt32(str) : 0;
15 }
16
17 /// <summary>
18 /// 判断对象是否为Int32类型的数字
19 /// </summary>
20 /// <param name="Expression"></param>
21 /// <returns></returns>
22 public static bool IsNumeric(object expression)
23 {
24 if (expression != null)
25 return IsNumeric(expression.ToString());
26
27 return false;
28
29 }
30
31 /// <summary>
32 /// 判断对象是否为Int32类型的数字
33 /// </summary>
34 /// <param name="Expression"></param>
35 /// <returns></returns>
36 public static bool IsNumeric(string expression)
37 {
38 if (expression != null)
39 {
40 string str = expression;
41 if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
42 {
43 if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
44 return true;
45 }
46 }
47 return false;
48 }
使用方法如下:
代码
1 private void Bind()
2 {
3 SQLHelper db = new SQLHelper(g_ConnStr);
4 db.Open();
5 DataSet ds = db.ExecuteDataSet("SELECT * FROM [DCMS_Article] where channel_id=16 order by art_date desc");
6
7 if (ds.Tables[0].Rows.Count != 0)
8 {
9 int nPageSize = 5;
10 int nShowNum = 5;
11 int nRecord=ds.Tables[0].Rows.Count;
12 int CurPage = Utils.ToInt(Request.QueryString["page"]);
13 PagedDataSource pds = new PagedDataSource();
14 pds.AllowPaging = true;
15 pds.DataSource = ds.Tables[0].DefaultView;
16 pds.PageSize = nPageSize;
17 int PageCount = pds.PageCount;
18 CurPage = CurPage == 0 ? 1 : CurPage;
19 CurPage = CurPage > PageCount ? PageCount : CurPage;
20 pds.CurrentPageIndex = CurPage - 1;
21 Repeater1.DataSource = pds;
22 lblPageLinks.Text = PageCount > 0 ? Utils.GetPageLinks(nRecord, nPageSize, nShowNum) : "";
23 }
24 else
25 {
26 Repeater1.DataSource = ds;
27 }
28 Repeater1.DataBind();
29 db.Close();
30 }
2 {
3 SQLHelper db = new SQLHelper(g_ConnStr);
4 db.Open();
5 DataSet ds = db.ExecuteDataSet("SELECT * FROM [DCMS_Article] where channel_id=16 order by art_date desc");
6
7 if (ds.Tables[0].Rows.Count != 0)
8 {
9 int nPageSize = 5;
10 int nShowNum = 5;
11 int nRecord=ds.Tables[0].Rows.Count;
12 int CurPage = Utils.ToInt(Request.QueryString["page"]);
13 PagedDataSource pds = new PagedDataSource();
14 pds.AllowPaging = true;
15 pds.DataSource = ds.Tables[0].DefaultView;
16 pds.PageSize = nPageSize;
17 int PageCount = pds.PageCount;
18 CurPage = CurPage == 0 ? 1 : CurPage;
19 CurPage = CurPage > PageCount ? PageCount : CurPage;
20 pds.CurrentPageIndex = CurPage - 1;
21 Repeater1.DataSource = pds;
22 lblPageLinks.Text = PageCount > 0 ? Utils.GetPageLinks(nRecord, nPageSize, nShowNum) : "";
23 }
24 else
25 {
26 Repeater1.DataSource = ds;
27 }
28 Repeater1.DataBind();
29 db.Close();
30 }
注意高亮的部分,我是写在类里的静态方法!用的PagedDataSource来实现的,GridView等数据绑定控件都可使用,参数可以自定义以方便同一个页面内的多次调用,支持多参数传递,你的页面可以是view.aspx?a=3&p=6&t=3&act=add&page=5&v=123等等,参数自动组合,手写干净代码的朋友有福了,当然你可以改进,以方便更简便的操作!