public string pageIndex()
{
int num = 0;//当前页码
if (string.IsNullOrWhiteSpace(Request["page"]) || Convert.ToInt32(Request["page"]) <= 1)
{
num = 1;
}
else
{
num = Convert.ToInt32(Request["page"]) ;
} StringBuilder sb = new StringBuilder();
con.Open();
string sql = "select count(id) from aa";
SqlCommand com = new SqlCommand(sql, con);
int count = Convert.ToInt32(com.ExecuteScalar());
con.Close();
int page = 0;//页总数
page =(int)Math.Ceiling((decimal)count / 20 );
int from = num / 10 + 1;
int to = num / 10 + 11;
for (int i = from; i < to && i < page; i++)
{
if (i == num)
sb.AppendLine("<strong>"+i+"</strong>");
else
sb.AppendLine("<a href='?page=" + i + "'>" + i + "</a>");
}
if (to - from >= 10)
{
sb.AppendLine("<a href='?page=" + (from + 10) + "'>...</a>");
}
if (num <= page)
{
sb.AppendLine("<a href='?page=" + (num +1) + "'>下一页</a>");
}
return sb.ToString();
}
问题出在from和to赋值上
当 点击的时候 当前页码变成url路径页码变成11
在计算 from的时候就会出现问题 int from =11/10+1这里居然不是11起始。。
更坑爹的是to这个时候是是12
怎么办呢
解决方案 »
- 命名空间“System.Web”中不存在类型或命名空间名称“HttpContext”(是缺少程序集引用)
- 打开新页面并传递多个参数??
- 一个ajax 有趣的问题。
- 分数押宝输光了,大家见谅,没多少分
- 急聘网站开发高手(.NET)
- ★★★ 如何不要路径 只要文件名 ★★★
- 2.0 母版页 怎么实现用DIV 来分块! 急 谢谢
- 大家来看看,为什么我的javascript调用不了这个Web服务?
- 请看看这种功能如何实现?
- 快放假了,你们假期里还要加班吗?顺便散分。
- 流量统计使用图片实现统计原理?
- sqlserver 建数据库时表示时间,金额和重量的字段用什么数据类型好,有人说全用nvarchar因为好操作,求解
int page = ceiling(1.0*count/pageSize)
int from = (num - 1) * pageSize +1
int to = from + pageSize -1
更坑爹的是to这个时候是是12
怎么办呢-------------------------
传过11当然不是11起始 int from = num / 10 + 1;
int to = num / 10 + 11;
from是2 to是12 当然是这样的结果
改下 int from=num/12+11; //这样不就11为起始页了么 不知道你to想为什么值 自己试着改改上面这两句
是什么意思?
pageSize 分页大小,每页显示的数据量
if(count%pageSize>0)page++;//计算总页数int from = (num - 1) * pageSize +1;//计算起始位置
int to = from + pageSize -1;//终止位置我测试过的,可能,你没写好吧。
http://blog.csdn.net/wszhoho/article/category/299268
pagesize=20 num是url页码 每次都成倍增长。
public string pageIndex()
{
int num = 0;//当前页码
if (string.IsNullOrWhiteSpace(Request["page"]) || Convert.ToInt32(Request["page"]) <= 1)
{
num = 1;
}
else
{
num = Convert.ToInt32(Request["page"]) ;
} StringBuilder sb = new StringBuilder();
con.Open();
string sql = "select count(id) from aa";
SqlCommand com = new SqlCommand(sql, con);
int count = Convert.ToInt32(com.ExecuteScalar());
con.Close();
int page = 0;//页总数
page =(int)Math.Ceiling((decimal)count / 20 );
int from = (num/10)*10 + 1;
int to = from+10;if(to>page){
to=page;
}
for (int i = from; i < to && i < page; i++)
{
if (i == num)
sb.AppendLine("<strong>"+i+"</strong>");
else
sb.AppendLine("<a href='?page=" + i + "'>" + i + "</a>");
}
if (to - from >= 10)
{
sb.AppendLine("<a href='?page=" + to + "'>...</a>");
}
if (num <= page)
{
sb.AppendLine("<a href='?page=" + (num +1) + "'>下一页</a>");
}
return sb.ToString();
}
int to = from+10; //当前分段的终止页码// 如果终止页码大于总页数,则当前分段的终止页码为总页数
if(to>page){
to=page;
}
/// 分页导航菜单 {0} 代表页码 {2}代表搜索关键字
/// </summary>
/// <param name="_AllPage">总页码</param>
/// <param name="NowPage">索引页码</param>
/// <param name="SearchKey">搜索关键字</param>
/// <returns></returns>
public static String MenuUrl(String TempLate, int AllPage, int NowPage, int PageLength, String SearchKey)
{
TempLate = TempLate.Replace("{2}", SearchKey);
StringBuilder StrPage = new StringBuilder();
StrPage.Append("当前页 ");
if (NowPage < 1)
{
NowPage = 1;
}
StrPage.Append(NowPage.ToString() + "/" + AllPage.ToString());
StrPage.Append("<a href=\"" + String.Format(TempLate, 1) + "\" class=\"MenuArticle\">« 首页</a>");
StrPage.Append((NowPage <= 1 ? "<a class=\"disabled\">« 上一页 </a>" : "<a href=\"" + String.Format(TempLate, (NowPage - 1).ToString()) + "\" class=\"MenuArticle\">« 上一页</a>"));
int StartI = (NowPage - PageLength) > 0 ? (NowPage - PageLength) : 1;
int EndI = (NowPage + PageLength) < AllPage ? (NowPage + PageLength) : AllPage;
for (int i = StartI; i <= EndI; i++)
{
if (i == NowPage)
{
StrPage.Append("<a class=\"number current\">" + i.ToString() + "</a>");
}
else
{
StrPage.Append("<a href=\"" + String.Format(TempLate, i.ToString()) + "\" class=\"number\">" + i.ToString() + "</a>");
}
}
StrPage.Append((NowPage >= AllPage ? "<a class=\"disabled\"> 下一页 »</a>" : "<a href=\"" + String.Format(TempLate, (NowPage + 1).ToString()) + "\" class=\"MenuArticle\">下一页</a>"));
StrPage.Append("<a href=\"" + String.Format(TempLate, AllPage.ToString()) + "\" class=\"MenuArticle\">末页 »</a>");
return StrPage.ToString();
}
{
int num = 0;//当前页码
if (string.IsNullOrWhiteSpace(Request["page"]) || Convert.ToInt32(Request["page"]) <= 1)
{
num = 1;
}
else
{
num = Convert.ToInt32(Request["page"]) ;
} StringBuilder sb = new StringBuilder();
con.Open();
string sql = "select count(id) from aa";
SqlCommand com = new SqlCommand(sql, con);
int count = Convert.ToInt32(com.ExecuteScalar());
con.Close();
int page = 0;//页总数
page =(int)Math.Ceiling((decimal)count / 20 );
int from = (num/10)*10 + 1; // 当前分段的起始页码
int to = from+9; //当前分段的终止页码// 如果终止页码大于总页数,则当前分段的终止页码为总页数
if(to>page){
to=page;
}/ 前一个分段
if (from>10)
{
sb.AppendLine("<a href='?page=" + (from-1) + "'>...</a>");
}//显示分段页码
for (int i = from; i <= to && i <= page; i++)
{
if (i == num)
sb.AppendLine("<strong>"+i+"</strong>");
else
sb.AppendLine("<a href='?page=" + i + "'>" + i + "</a>");
}// 后一个分段
if (to<page)
{
sb.AppendLine("<a href='?page=" + (to+1) + "'>...</a>");
} if (num <= page)
{
sb.AppendLine("<a href='?page=" + (num +1) + "'>下一页</a>");
}
return sb.ToString();
}
if (from>10)
{
sb.AppendLine("<a href='?page=" + (from-1) + "'>...</a>");
}这个还不对,楼主去掉这个吧。
// 前一个分段应这样算
if (from>10)
{
sb.AppendLine("<a href='?page=" + ((from-10)/10)*10+1) + "'>...</a>");
}
public string pageIndex()
{
int num = 0;//当前页码
if (string.IsNullOrWhiteSpace(Request["page"]) || Convert.ToInt32(Request["page"]) <= 1)
{
num = 1;
}
else
{
num = Convert.ToInt32(Request["page"]) ;
} StringBuilder sb = new StringBuilder();
con.Open();
string sql = "select count(id) from aa";
SqlCommand com = new SqlCommand(sql, con);
int count = Convert.ToInt32(com.ExecuteScalar());
con.Close();
int page = 0;//页总数
page = (int)Math.Ceiling((decimal) count / 20);
int from = num / 10 * 10 + 1;//起始页
int to = num / 10 * 10 + 11;//终止页 for (int i = from; i < to && i < page; i++)
{
if (i == num)
sb.AppendLine("<strong>"+i+"</strong>");
else
sb.AppendLine("<a href='?page=" + i + "'>" + i + "</a>");
}
if (to - from >= 10)
{
sb.AppendLine("<a href='?page=" + (from + 10) + "'>...</a>");
}
if (num <= page)
{
sb.AppendLine("<a href='?page=" + (num +1) + "'>下一页</a>");
}
return sb.ToString();
}
还要加一个判断:int from = (num/10)*10 + 1;
if(from%10==0&from>=10){
from=(num/10-1)*10 + 1;
}