由于文章的数量小所以我一页就只让它显示出三条记录,你可以跟据代码设置,
当然为了灵活起见,你可把这个放到显示几条记录的值存入数据库当中,以后想设成多少页这也挺方便的.
![](https://i-blog.csdnimg.cn/blog_migrate/86de23e959e16fdb9e6bd7ce7add99b4.gif)
(图1)
![](https://i-blog.csdnimg.cn/blog_migrate/1888c5584a1a30909030eef203969977.gif)
(图2)
以前写的那个用 ajax 实现无刷新分页
可那时有点问题,那就是在下面没有固定显示哪些几页,而是一下子,把所有文章分页之后的页码全都显示出来了.
如果有五十页的话,那就会有1~50这些页显示在那里
而我们一般看到的都是在这里,只是看到如有50页但我只显示头页10的数字现来
当我点击其中一个数是,会跟这个数在这二个数当中显示的位置,把与之相近的十页的页码显示出来
在图一当中显示得是头十页的数,当我点击图1 当中的 9 时,显示出来的结果如图2所示
这样显示起来是不是要比以前那种好呢.
好了,下面我就把实现上面的代码贴出来.
肯定会有比我更好的法子
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
public
partial
class
Blog_moreArticle : basePage
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
protected void Page_Load(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (!IsPostBack)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
AjaxPro.Utility.RegisterTypeForAjax(typeof(Blog_moreArticle));
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**////文章总数
articleCount = moreArticle.articleCount;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 每页显示多少条记录
/// </summary>
private static int _pageNumber = 3;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 一页显示多少条记录
/// </summary>
private static int pageNumber
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
get ...{ return _pageNumber; }
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static int _count = 1;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 要取第几页
/// </summary>
private static int count
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
get ...{ return _count; }
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
set ...{ _count = value; }
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static int _articleCount = 1;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 文章总数
/// </summary>
private static int articleCount
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
get ...{ return _articleCount; }
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
set ...{ _articleCount = value; }
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private static int _pageCount = 0;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 有多少页
/// </summary>
private static int pageCount
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
get
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int t = articleCount % pageNumber; //求余数 53 % 10 = 3;
int v = articleCount;
if (t == 0) //如果余数为0,那么页数就不用加一
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
v = v / pageNumber;
return v;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
v = (v - t) / pageNumber + 1; //因为有余数,得把余数减去然后相除加一得出总页数
return v;
}
//return _userCount;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
set ...{ _pageCount = value; }
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
取文章排序#region 取文章排序
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 把文章取出来
/// </summary>
[AjaxPro.AjaxMethod]
public static string article()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return moreArticle.allArticle(pageNumber,count);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 标题排序
/// </summary>
/// <param name="taxis">排序</param>
/// <returns>string</returns>
[AjaxPro.AjaxMethod]
public static string artitle_title(int taxis)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return moreArticle.allArticle_title(pageNumber,count, taxis);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 按blog名排序
/// <summary>
/// <param name="taxis">排序</param>
/// <returns>string</returns>
[AjaxPro.AjaxMethod]
public static string artitle_blogName(int taxis)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return moreArticle.allArticle_blogName(pageNumber, count, taxis);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 按用户名排序
/// </summary>
/// <param name="taxis">排序</param>
/// <returns>string</returns>
[AjaxPro.AjaxMethod]
public static string article_userName(int taxis)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return moreArticle.allArticle_userName(pageNumber, count, taxis);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 按发表时间排序
/// </summary>
/// <param name="taxis">排序</param>
/// <returns>string</returns>
[AjaxPro.AjaxMethod]
public static string article_issueTime(int taxis)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return moreArticle.allArticle_issueTime(pageNumber, count, taxis);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 得到要翻多少页
/// </summary>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public static int getPageCount()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return pageCount;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 要读取第几页的数据
/// </summary>
/// <param name="apc"></param>
/// <returns>int</returns>
[AjaxPro.AjaxMethod]
public static void setArticlePageCount(int apc)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
count = apc;
}
}
这里用到了个basePage,只是自己写的一个基类,别的页都承继它代码如下
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
using
XiaoTuNi.LHB_SQL_2005;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/**/
/// <summary>
/// basePage 的摘要说明
/// </summary>
public
class
basePage :Page
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public basePage()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
}
private static string _accessUrl = "";
public static string accessUrl
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
get ...{ return _accessUrl; }
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
set ...{ _accessUrl = value;}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
protected override void OnLoad(EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//页的url地址
accessUrl = HttpContext.Current.Request.Url.ToString();
base.OnLoad(e);
}
}
里面还有一个类就是moreArticle.cs类,这个类主要是用来取数据用的.代码在下面.
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Text;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/**/
/// <summary>
/// moreArticle 的摘要说明
/// </summary>
public
class
moreArticle : Operator
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public moreArticle()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
取文章#region 取文章
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// sql语句
/// </summary>
private static string sqlString(int articleNumber, int count)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return "SELECT ArticleID, blogName, userName, title, issueTime FROM uavPage WHERE number> " + articleNumber * (count - 1) + " AND number <= " + articleNumber * count;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 文章内容
/// </summary>
/// <param name="executeString">sql语句或存储过程名称</param>
/// <returns>string</returns>
private static string articleDate(string executeString)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
StringBuilder articleId = new StringBuilder();
StringBuilder blogName = new StringBuilder();
StringBuilder userName = new StringBuilder();
StringBuilder title = new StringBuilder();
StringBuilder issueTime = new StringBuilder();
IDataReader idr = ExecuteReader(executeString);
while (idr.Read())
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
articleId.Append(idr["articleId"].ToString()).Append("//");
blogName.Append(idr["blogName"].ToString()).Append("//");
userName.Append(idr["userName"].ToString()).Append("//");
title.Append(idr["title"].ToString()).Append("//");
issueTime.Append(idr["issueTime"].ToString()).Append("//");
}
idr.Close();
string temp = articleId.ToString() + "|||" + blogName.ToString() + "|||" + userName.ToString() + "|||" + title.ToString() + "|||" + issueTime.ToString();
return temp;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 把文章取出来
/// </summary>
public static string allArticle(int articleNumber,int count)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
string executeString = sqlString(articleNumber, count) + " order by articleID desc";
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
return articleDate(executeString);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 按blog名排序
/// <summary>
/// <param name="taxis">排序</param>
/// <returns>string</returns>
public static string allArticle_blogName(int articleNumber, int count, int taxis)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
string executeString = "";
if (taxis == 1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
executeString = sqlString(articleNumber, count) + " ORDER BY blogName";
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
executeString = sqlString(articleNumber, count) + " ORDER BY blogName desc";
}
return articleDate(executeString);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 按用户名排序
/// </summary>
/// <param name="taxis">排序</param>
/// <returns>string</returns>
public static string allArticle_userName(int articleNumber, int count, int taxis)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
string executeString = "";
if (taxis == 1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
executeString = sqlString(articleNumber, count) + " ORDER BY userName";
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
executeString = sqlString(articleNumber, count) + " ORDER BY userName desc";
}
return articleDate(executeString);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 标题排序
/// </summary>
/// <param name="taxis">排序</param>
/// <returns>string</returns>
public static string allArticle_title(int articleNumber, int count, int taxis)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
string executeString = "";
if (taxis == 1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
executeString = sqlString(articleNumber, count) + " ORDER BY title";
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
executeString = sqlString(articleNumber, count) + " ORDER BY title desc";
}
return articleDate(executeString);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 按发表时间排序
/// </summary>
/// <param name="taxis">排序</param>
/// <returns>string</returns>
public static string allArticle_issueTime(int articleNumber, int count, int taxis)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
string executeString = "";
if (taxis == 1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
executeString = sqlString(articleNumber, count) + " ORDER BY issueTime";
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
executeString = sqlString(articleNumber, count) + " ORDER BY issueTime desc";
}
return articleDate(executeString);
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
#endregion 取文章
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 文章总数
/// </summary>
public static int articleCount
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
get
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
string executeString = " select Max(number) from uavPage";
int count = int.Parse(ExecuteScalar(executeString) );
return count;
}
}
}