ASP.net 简单分页的实现

在自己的项目中有一个文章的管理页面需要用到分页,

这种分页方法是在黑马的一个视频中看到的,便用在了自己的项目中。

但是使用控件实在是太丑,虽然我写的也丑......。

gridview 控件提供的分页已经足够强大,微小的数据量根本不需要在意。

而本文所介绍的分页一样无法应对大量的数据,只是一种简单的实现。

通过传入的 请求页码 以及程序设定好的 每页显示的数量 ,计算出一页数据的开始数与结束数,调用 GetPageList 方法,返回泛型数据集合。

 1         //pageindex:页码,paesize:每页数量
 2 
 3 
 4         public static List<ArticleBLL> GetArticleMenu(int pageindex, int pagesize)
 5         {
 6             int start = (pageindex - 1) * pagesize + 1;
 7             int end = pagesize * pageindex;
 8             List<ArticleBLL> list = GetPageList(start, end);
 9             return list;
10         }


GetPageList 方法,接收到上面方法的计算结果,查询数据然后返回。

 1         // 传入 开始id 以及 结束id 获得数据列表
 2 
 3 
 4         private static List<ArticleBLL> GetPageList(int start, int end)
 5         {
 6             string sqlstring = "select * from (select row_number() over(order by Time desc) as num,* from Article)as t where t.num >= @start and t.num <= @end";
 7             SqlParameter[] para = {
 8                 new SqlParameter ("@start",start),
 9                 new SqlParameter("@end",end)
10             };
11             DBHelp db = new DBHelp();
12             DataTable dt = db.GetDataTable(sqlstring, para);
13             List<ArticleBLL> list = null;
14             if (dt.Rows.Count > 0)
15             {
16                 list = new List<ArticleBLL>();
17                 ArticleBLL article = null;
18                 foreach (DataRow row in dt.Rows)
19                 {
20                     article = new ArticleBLL();
21                     LoadEnity(row, article);
22                     list.Add(article);
23                 }
24             }
25             return list;
26         }

转化数据。

1         //转化数据
2         private static void LoadEnity(DataRow row, ArticleBLL article)
3         {
4             article.ID = Convert.ToInt32(row["ID"]);
5             article.Title = Convert.ToString(row["Title"]);
6             article.Text = Convert.ToString(row["Text"]);
7             article.Time = Convert.ToDateTime(row["Time"]);
8         }

生成页码条

我使用的前端框架是 Bootstrap v2 在这里直接将样式写在输出里了。

 1         //产生数字页码条
 2         //页面产生10个数字页码
 3         //pageindex  传来的请求页码值
 4         //articlemenucount  总页码数量
 5         //pagesize  每页数量值
 6 
 7 
 8         public static string GetPageBar(int pageindex,int pagesize)
 9         {
10             int articlemenucount = GetArticleMenuCount(pagesize);
11             if (articlemenucount == 1)
12             { return string.Empty; }
13             int start = pageindex - 5;
14             if (start < 1)
15             { start = 1; }
16             int end = start + pagesize - 1;
17             if (end > articlemenucount)
18             { end = articlemenucount; }
19             StringBuilder pagebar = new StringBuilder();
20             pagebar.Append("<div class="pagination pagination-right"><ul>");
21             for (int i = start; i <= end; i++)
22             {
23                 if (i == pageindex)
24                 { pagebar.Append("<li class="active"><a>" + i +"</a></li>"); }
25                 else
26                 { pagebar.Append(string.Format("<li><a href='arctilemenu.aspx?page={0}'>{0}</a></li>",i)); }
27             }
28             pagebar.Append("</ul></div>");
29             return pagebar.ToString();
30         }


这两个方法求出数据的总条数,然后计算全部的页数。供上面方法计算页码条页码。

1         //求总页数
2 
3 
4         private static int GetArticleMenuCount(int pagesize)
5         {
6             int rowcount = GetRowCount();
7             int pagecount = Convert.ToInt32(Math.Ceiling((double)rowcount / pagesize));
8             return pagecount;
9         }
1         //求出总记录数
2         private static int GetRowCount()
3         {
4             string sqlstring = "select count(*) from Article";
5             DBHelp db = new DBHelp();
6             return Convert.ToInt32(db.GetInt(sqlstring));
7         }

 在调用时只需要传入每页的数据条数以及当前请求的页码就可以了。

 最后,真的是好丑啊。



转载请标明出处

作者:AaXuan

地址:http://www.cnblogs.com/Aaxuan

知识共享许可协议

本作品采用  知识共享署名 3.0 未本地化版本许可协议  进行许可。

原文地址:https://www.cnblogs.com/Aaxuan/p/6676815.html