asp.net aspnetpager控件分页统计数据不准确的解决方法

发布时间:2019-11-24编辑:脚本学堂
用asp.net的aspnetpager控件实现的分页代码,出现统计数据与实际不符的问题。原因在于以前都是在页数方面直接用数字写进去,其实应该写成anPager.PageSize=10;的形式,这样的话,统计数字就与实际相符了。

用asp.net的aspnetpager控件实现的分页代码,出现统计数据与实际不符的问题。
原因在于以前都是在页数方面直接用数字写进去,其实应该写成anPager.PageSize=10;的形式,这样的话,统计数字就与实际相符了。
以下是修改后的正确代码,供大家学习参考。
 

复制代码 代码示例:

/// <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();
}