asp.net aspnetpager分頁統(tǒng)計(jì)時(shí)與實(shí)際不符的解決辦法
/// <summary>
/// 需要分頁時(shí)使用,根據(jù)參數(shù)和ConditionExpress獲取DataTable
/// </summary>
/// <param name="_tableName">表名</param>
/// <param name="_fieldNames">字段名集合,用逗號(hào)分開</param>
/// <param name="_OrderColumn">排序字段,用于統(tǒng)計(jì)有多少條記錄</param>
/// <param name="IsDesc">是否倒序</param>
/// <param name="_indexColumn">自增字段名</param>
/// <param name="_currentPage">當(dāng)前頁</param>
/// <param name="pageSize">頁大小</param>
/// <param name="_rowsCount">總記錄數(shù)</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;
//取得符合條件的數(shù)據(jù)總數(shù)
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);
}
}
}
調(diào)用如下:
private void bind()
{
int rowCount = 1;
string wherestr = string.Empty;
//設(shè)置分頁
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();
}
之前一直在頁數(shù)方面直接用數(shù)字寫進(jìn)去,沒有寫成anPager.PageSize=10;的形式,在老湯的提醒下,做了修改,也解決了一直困擾我的問題。
- ASP制作在線人數(shù)統(tǒng)計(jì)實(shí)例
- 統(tǒng)計(jì)有多少行JS代碼和ASP代碼
- ASP程序代碼執(zhí)行時(shí)間統(tǒng)計(jì)類
- asp論壇在線人數(shù)統(tǒng)計(jì)研究
- 實(shí)現(xiàn)ASP程序執(zhí)行時(shí)間統(tǒng)計(jì)類的代碼
- asp實(shí)現(xiàn)一個(gè)統(tǒng)計(jì)當(dāng)前在線用戶的解決方案
- ASP訪問數(shù)量統(tǒng)計(jì)代碼
- asp.net中調(diào)用Office來制作3D統(tǒng)計(jì)圖的實(shí)例代碼
- ASP.net中網(wǎng)站訪問量統(tǒng)計(jì)方法代碼
- php模仿asp Application對(duì)象在線人數(shù)統(tǒng)計(jì)實(shí)現(xiàn)方法
- 四步完成asp網(wǎng)頁設(shè)計(jì)流量統(tǒng)計(jì)
相關(guān)文章
asp.net 純真ip庫取得所在地實(shí)現(xiàn)代碼
asp.net 純真ip庫取得所在地實(shí)現(xiàn)代碼,需要的朋友可以參考一下。2009-05-05asp.net 2個(gè)日期之間的整月數(shù)的算法
我是說兩個(gè)日期之間間隔整月,比如2008-11-5 和 2009-4-3之間的整月,結(jié)果是12,1,2,3這四個(gè)月2009-06-06.NET調(diào)用控制臺(tái)下生成的exe文件,傳參及獲取返回參數(shù)的思路及代碼
.NET調(diào)用控制臺(tái)下生成的exe文件,傳參及獲取返回參數(shù)的思路及代碼,需要的朋友可以參考一下2013-06-06.Net?Core?進(jìn)程守護(hù)之Supervisor使用詳解
這篇文章主要介紹了.Net?Core?進(jìn)程守護(hù)之Supervisor使用,Supervisor它可以很方便的監(jiān)聽、啟動(dòng)、停止、重啟一個(gè)或多個(gè)進(jìn)程,對(duì).Net?Core?進(jìn)程守護(hù)之Supervisor使用相關(guān)知識(shí)感興趣的朋友一起看看吧2022-04-04ASP.NET?Core設(shè)置Ocelot網(wǎng)關(guān)限流
這篇文章介紹了ASP.NET?Core設(shè)置Ocelot網(wǎng)關(guān)限流的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04Asp.Net+XML操作基類(修改,刪除,新增,創(chuàng)建)
更新內(nèi)容: 1,根據(jù)父節(jié)點(diǎn)屬性讀取字節(jié)點(diǎn)值 2,根據(jù)節(jié)點(diǎn)屬性讀取子節(jié)點(diǎn)值(較省資源模式)2008-07-07asp.net使用FCK編輯器中的分頁符實(shí)現(xiàn)長(zhǎng)文章分頁功能
這篇文章主要介紹了asp.net使用FCK編輯器中的分頁符實(shí)現(xiàn)長(zhǎng)文章分頁功能,涉及asp.net字符串及分頁操作的相關(guān)技巧,需要的朋友可以參考下2016-06-06asp.net GridView 刪除時(shí)彈出確認(rèn)對(duì)話框(包括內(nèi)容提示)
GridView 刪除時(shí)彈出確認(rèn)對(duì)話框(包括內(nèi)容提示)2009-12-12asp.net Repeater取得CheckBox選中的某行某個(gè)值的c#寫法
asp.net(c#)利用Repeater取得CheckBox選中行的某個(gè)值的代碼2008-08-08