由于文章的数量小所以我一页就只让它显示出三条记录,你可以跟据代码设置,
当然为了灵活起见,你可把这个放到显示几条记录的值存入数据库当中,以后想设成多少页这也挺方便的.
![](http://hiphotos.baidu.com/xiaotuni/pic/item/9006eff8ed24150ed9f9fdc8.jpg)
(图1)
![](http://hiphotos.baidu.com/xiaotuni/pic/item/fcba1e4ca9679df8d72afcc8.jpg)
(图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;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public partial class Blog_moreArticle : basePage
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
protected void Page_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if (!IsPostBack)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
AjaxPro.Utility.RegisterTypeForAjax(typeof(Blog_moreArticle));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////文章总数
articleCount = moreArticle.articleCount;
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 每页显示多少条记录
/// </summary>
private static int _pageNumber = 3;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 一页显示多少条记录
/// </summary>
private static int pageNumber
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get ...{ return _pageNumber; }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static int _count = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 要取第几页
/// </summary>
private static int count
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get ...{ return _count; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
set ...{ _count = value; }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static int _articleCount = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 文章总数
/// </summary>
private static int articleCount
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get ...{ return _articleCount; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
set ...{ _articleCount = value; }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static int _pageCount = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 有多少页
/// </summary>
private static int pageCount
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
int t = articleCount % pageNumber; //求余数 53 % 10 = 3;
int v = articleCount;
if (t == 0) //如果余数为0,那么页数就不用加一
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
v = v / pageNumber;
return v;
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
v = (v - t) / pageNumber + 1; //因为有余数,得把余数减去然后相除加一得出总页数
return v;
}
//return _userCount;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
set ...{ _pageCount = value; }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
取文章排序#region 取文章排序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 把文章取出来
/// </summary>
[AjaxPro.AjaxMethod]
public static string article()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return moreArticle.allArticle(pageNumber,count);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 标题排序
/// </summary>
/// <param name="taxis">排序</param>
/// <returns>string</returns>
[AjaxPro.AjaxMethod]
public static string artitle_title(int taxis)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return moreArticle.allArticle_title(pageNumber,count, taxis);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 按blog名排序
/// <summary>
/// <param name="taxis">排序</param>
/// <returns>string</returns>
[AjaxPro.AjaxMethod]
public static string artitle_blogName(int taxis)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return moreArticle.allArticle_blogName(pageNumber, count, taxis);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 按用户名排序
/// </summary>
/// <param name="taxis">排序</param>
/// <returns>string</returns>
[AjaxPro.AjaxMethod]
public static string article_userName(int taxis)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return moreArticle.allArticle_userName(pageNumber, count, taxis);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 按发表时间排序
/// </summary>
/// <param name="taxis">排序</param>
/// <returns>string</returns>
[AjaxPro.AjaxMethod]
public static string article_issueTime(int taxis)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return moreArticle.allArticle_issueTime(pageNumber, count, taxis);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 得到要翻多少页
/// </summary>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public static int getPageCount()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return pageCount;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 要读取第几页的数据
/// </summary>
/// <param name="apc"></param>
/// <returns>int</returns>
[AjaxPro.AjaxMethod]
public static void setArticlePageCount(int apc)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.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;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using XiaoTuNi.LHB_SQL_2005;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// basePage 的摘要说明
/// </summary>
public class basePage :Page
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
public basePage()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
}
private static string _accessUrl = "";
public static string accessUrl
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get ...{ return _accessUrl; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
set ...{ _accessUrl = value;}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected override void OnLoad(EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.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;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// moreArticle 的摘要说明
/// </summary>
public class moreArticle : Operator
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
public moreArticle()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
取文章#region 取文章
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// sql语句
/// </summary>
private static string sqlString(int articleNumber, int count)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return "SELECT ArticleID, blogName, userName, title, issueTime FROM uavPage WHERE number> " + articleNumber * (count - 1) + " AND number <= " + articleNumber * count;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 文章内容
/// </summary>
/// <param name="executeString">sql语句或存储过程名称</param>
/// <returns>string</returns>
private static string articleDate(string executeString)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.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())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.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;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 把文章取出来
/// </summary>
public static string allArticle(int articleNumber,int count)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
string executeString = sqlString(articleNumber, count) + " order by articleID desc";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return articleDate(executeString);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 按blog名排序
/// <summary>
/// <param name="taxis">排序</param>
/// <returns>string</returns>
public static string allArticle_blogName(int articleNumber, int count, int taxis)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
string executeString = "";
if (taxis == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
executeString = sqlString(articleNumber, count) + " ORDER BY blogName";
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
executeString = sqlString(articleNumber, count) + " ORDER BY blogName desc";
}
return articleDate(executeString);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 按用户名排序
/// </summary>
/// <param name="taxis">排序</param>
/// <returns>string</returns>
public static string allArticle_userName(int articleNumber, int count, int taxis)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
string executeString = "";
if (taxis == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
executeString = sqlString(articleNumber, count) + " ORDER BY userName";
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
executeString = sqlString(articleNumber, count) + " ORDER BY userName desc";
}
return articleDate(executeString);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 标题排序
/// </summary>
/// <param name="taxis">排序</param>
/// <returns>string</returns>
public static string allArticle_title(int articleNumber, int count, int taxis)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
string executeString = "";
if (taxis == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
executeString = sqlString(articleNumber, count) + " ORDER BY title";
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
executeString = sqlString(articleNumber, count) + " ORDER BY title desc";
}
return articleDate(executeString);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 按发表时间排序
/// </summary>
/// <param name="taxis">排序</param>
/// <returns>string</returns>
public static string allArticle_issueTime(int articleNumber, int count, int taxis)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
string executeString = "";
if (taxis == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
executeString = sqlString(articleNumber, count) + " ORDER BY issueTime";
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
executeString = sqlString(articleNumber, count) + " ORDER BY issueTime desc";
}
return articleDate(executeString);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#endregion 取文章
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 文章总数
/// </summary>
public static int articleCount
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
string executeString = " select Max(number) from uavPage";
int count = int.Parse(ExecuteScalar(executeString) );
return count;
}
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)