asp.net aspnetpager分页统计时与实际不符的解决办法

 更新时间:2008年11月27日 14:21:34   作者:  
最近分页方面根据实际需要修改了一些函数
基本函数如下:
复制代码 代码如下:

/// <summary>
/// 需要分页时使用,根据参数和ConditionExpress获取DataTable
/// </summary>
/// <param name="_tableName">表名</param>
/// <param name="_fieldNames">字段名集合,用逗号分开</param>
/// <param name="_OrderColumn">排序字段,用于统计有多少条记录</param>
/// <param name="IsDesc">是否倒序</param>
/// <param name="_indexColumn">自增字段名</param>
/// <param name="_currentPage">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="_rowsCount">总记录数</param>
/// <returns>获取到的DataTable</returns>
public static DataTable GetDataTable(string _tableName, string _fieldNames, string _OrderColumn, bool IsDesc, string _indexColumn, int _currentPage, int pageSize, string conditionExpress, ref int _rowsCount)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
string whereStr = " where 1=1 ";
string sort = IsDesc ? " desc" : " asc";

string sqlStr = " from " + _tableName;
//排序字段
string orderStr = " order by " + _OrderColumn + sort;
if (_OrderColumn != _indexColumn)
orderStr += "," + _indexColumn + sort;
if (conditionExpress != string.Empty)
{
whereStr += conditionExpress;
}
sqlStr += whereStr;

//取得符合条件的数据总数
SqlCommand cmd = new SqlCommand("select count(" + _OrderColumn + ") " + sqlStr, conn);
conn.Open();
try
{
_rowsCount = (int)cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}

if (_currentPage > _rowsCount) _currentPage = _rowsCount;

if (_currentPage > 1)
{
if (IsDesc)
sqlStr += " and " + _OrderColumn + " < (select MIN(" + _OrderColumn + ") from ";
else
sqlStr += " and " + _OrderColumn + " > (select MAX(" + _OrderColumn + ") from ";
sqlStr += "(select top " + (pageSize * (_currentPage - 1)) + " " + _OrderColumn + " from " + _tableName + whereStr + orderStr + ") as t)";
}
sqlStr = "select top " + pageSize + " " + _fieldNames + sqlStr + orderStr;

try
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlStr, conn);
da.Fill(ds);
return ds.Tables[0];
}
catch (Exception EX)
{
throw new Exception(EX.Message);
}
}
}

调用如下:
复制代码 代码如下:

private void bind()
{
int rowCount = 1;
string wherestr = string.Empty;
//设置分页
anPager.AlwaysShow = true;
anPager.PageSize = 10;
this.rptdictionary.DataSource = GetDataTable(
"dictionary_Toysgogo_",
"[id_dictionary_],[namecn_dictionary_],[nameen_dictionary_],[point_dictionary_]",
"[id_dictionary_]",
true,
"[id_dictionary_]",
this.anPager.CurrentPageIndex,
anPager.PageSize,
wherestr,
ref rowCount
);
this.anPager.RecordCount = rowCount;
this.rptdictionary.DataBind();
}

复制代码 代码如下:

//分页切换
protected void anPager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
this.anPager.CurrentPageIndex = e.NewPageIndex;
this.tbxType.Text = this.tbxType.Text;
bind();
}

之前一直在页数方面直接用数字写进去,没有写成anPager.PageSize=10;的形式,在老汤的提醒下,做了修改,也解决了一直困扰我的问题。

相关文章

  • 用Html5与Asp.net MVC上传多个文件的实现代码

    用Html5与Asp.net MVC上传多个文件的实现代码

    Html 5 的有一些File API,对Form表单增强的特性,让我们轻松支持多文件上传,看下面的Html片断代码
    2012-08-08
  • .net中自定义错误页面的实现方法

    .net中自定义错误页面的实现方法

    这篇文章主要给大家介绍了关于.net中自定义错误页面实现的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-06-06
  • asp.net下加密Config的方法

    asp.net下加密Config的方法

    本文说明使用RSAProtectedConfigurationProvidert和计算机级别的密钥容器进行加密的步骤。
    2011-02-02
  • .NET9 AOT部署方案详解

    .NET9 AOT部署方案详解

    文章详细介绍了.NET AOT(Ahead-of-Time)和JIT(Just-in-Time)两种编译和部署方式的对比,包括它们的优点和缺点,AOT适合快速启动、内存占用低、性能稳定的场景,这篇文章主要介绍了.NET9 AOT部署,需要的朋友可以参考下
    2025-01-01
  • asp net core 2.1中如何使用jwt(从原理到精通)

    asp net core 2.1中如何使用jwt(从原理到精通)

    这篇文章主要给大家介绍了关于asp net core 2.1中如何使用jwt(从原理到精通)的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧
    2018-11-11
  • ASP.NET Core使用MiniProfiler分析应用

    ASP.NET Core使用MiniProfiler分析应用

    这篇文章介绍了ASP.NET Core使用MiniProfiler分析应用的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-02-02
  • VS2015自带LocalDB数据库用法详解

    VS2015自带LocalDB数据库用法详解

    这篇文章主要为大家详细介绍了VS2015自带LocalDB数据库的用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • .NET 下运用策略模式(组合行为和实体的一种模式)

    .NET 下运用策略模式(组合行为和实体的一种模式)

    我简单的理解策略模式就是把行为(方法)单独的抽象出来,并采用组合(Has-a)的方式,来组合行为和实体的一种模式比如,.NET中对数组排序的Sort的方法就是一个策略模式的实现模板
    2012-12-12
  • ASP.NET Core 3.0迁移的完美避坑指南

    ASP.NET Core 3.0迁移的完美避坑指南

    这篇文章主要给大家介绍了关于ASP.NET Core 3.0迁移的完美避坑指南,文中通过示例代码介绍的非常详细,对大家学习或者使用ASP.NET Core 3.0具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-09-09
  • .net压缩功能实现方法

    .net压缩功能实现方法

    这篇文章主要介绍了.net压缩功能实现方法,需要的朋友可以参考下
    2014-02-02

最新评论