asp.Netaspnetpagerページング統計時に実際と一致しない解決方法

3784 ワード

基本関数は次のとおりです.
 
  
///
/// , ConditionExpress DataTable
///

///
/// ,
/// ,
///
///
///
///
///
/// DataTable
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;の形式で、古いスープの注意の下で、修正をして、ずっと私を困らせている問題も解決しました.