由于文章的数量小所以我一页就只让它显示出三条记录,你可以跟据代码设置,
当然为了灵活起见,你可把这个放到显示几条记录的值存入数据库当中,以后想设成多少页这也挺方便的.
(图1)
(图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;
public partial class Blog_moreArticle : basePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(Blog_moreArticle));
///文章总数
articleCount = moreArticle.articleCount;
}
}
/// <summary>
/// 每页显示多少条记录
/// </summary>
private static int _pageNumber = 3;
/// <summary>
/// 一页显示多少条记录
/// </summary>
private static int pageNumber
{
get { return _pageNumber; }
}
private static int _count = 1;
/// <summary>
/// 要取第几页
/// </summary>
private static int count
{
get { return _count; }
set { _count = value; }
}
private static int _articleCount = 1;
/// <summary>
/// 文章总数
/// </summary>
private static int articleCount
{
get { return _articleCount; }
set { _articleCount = value; }
}
private static int _pageCount = 0;
/// <summary>
/// 有多少页
/// </summary>
private static int pageCount
{
get
{
int t = articleCount % pageNumber; //求余数 53 % 10 = 3;
int v = articleCount;
if (t == 0) //如果余数为0,那么页数就不用加一
{
v = v / pageNumber;
return v;
}
else
{
v = (v - t) / pageNumber + 1; //因为有余数,得把余数减去然后相除加一得出总页数
return v;
}
//return _userCount;
}
set { _pageCount = value; }
}
取文章排序
/// <summary>
/// 得到要翻多少页
/// </summary>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public static int getPageCount()
{
return pageCount;
}
/// <summary>
/// 要读取第几页的数据
/// </summary>
/// <param name="apc"></param>
/// <returns>int</returns>
[AjaxPro.AjaxMethod]
public static void setArticlePageCount(int apc)
{
count = apc;
}
}
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;
public partial class Blog_moreArticle : basePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(Blog_moreArticle));
///文章总数
articleCount = moreArticle.articleCount;
}
}
/// <summary>
/// 每页显示多少条记录
/// </summary>
private static int _pageNumber = 3;
/// <summary>
/// 一页显示多少条记录
/// </summary>
private static int pageNumber
{
get { return _pageNumber; }
}
private static int _count = 1;
/// <summary>
/// 要取第几页
/// </summary>
private static int count
{
get { return _count; }
set { _count = value; }
}
private static int _articleCount = 1;
/// <summary>
/// 文章总数
/// </summary>
private static int articleCount
{
get { return _articleCount; }
set { _articleCount = value; }
}
private static int _pageCount = 0;
/// <summary>
/// 有多少页
/// </summary>
private static int pageCount
{
get
{
int t = articleCount % pageNumber; //求余数 53 % 10 = 3;
int v = articleCount;
if (t == 0) //如果余数为0,那么页数就不用加一
{
v = v / pageNumber;
return v;
}
else
{
v = (v - t) / pageNumber + 1; //因为有余数,得把余数减去然后相除加一得出总页数
return v;
}
//return _userCount;
}
set { _pageCount = value; }
}
取文章排序
/// <summary>
/// 得到要翻多少页
/// </summary>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public static int getPageCount()
{
return pageCount;
}
/// <summary>
/// 要读取第几页的数据
/// </summary>
/// <param name="apc"></param>
/// <returns>int</returns>
[AjaxPro.AjaxMethod]
public static void setArticlePageCount(int apc)
{
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;
using XiaoTuNi.LHB_SQL_2005;
/// <summary>
/// basePage 的摘要说明
/// </summary>
public class basePage :Page
{
public basePage()
{
}
private static string _accessUrl = "";
public static string accessUrl
{
get { return _accessUrl; }
set { _accessUrl = value;}
}
protected override void OnLoad(EventArgs e)
{
//页的url地址
accessUrl = HttpContext.Current.Request.Url.ToString();
base.OnLoad(e);
}
}
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 XiaoTuNi.LHB_SQL_2005;
/// <summary>
/// basePage 的摘要说明
/// </summary>
public class basePage :Page
{
public basePage()
{
}
private static string _accessUrl = "";
public static string accessUrl
{
get { return _accessUrl; }
set { _accessUrl = value;}
}
protected override void OnLoad(EventArgs e)
{
//页的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;
/// <summary>
/// moreArticle 的摘要说明
/// </summary>
public class moreArticle : Operator
{
public moreArticle()
{
}
取文章 取文章
/// <summary>
/// 文章总数
/// </summary>
public static int articleCount
{
get
{
string executeString = " select Max(number) from uavPage";
int count = int.Parse(ExecuteScalar(executeString) );
return count;
}
}
}
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;
/// <summary>
/// moreArticle 的摘要说明
/// </summary>
public class moreArticle : Operator
{
public moreArticle()
{
}
取文章 取文章
/// <summary>
/// 文章总数
/// </summary>
public static int articleCount
{
get
{
string executeString = " select Max(number) from uavPage";
int count = int.Parse(ExecuteScalar(executeString) );
return count;
}
}
}