asp.net aspnetpager分頁(yè)統(tǒng)計(jì)時(shí)與實(shí)際不符的解決辦法
更新時(shí)間:2008年11月27日 14:21:34 作者:
最近分頁(yè)方面根據(jù)實(shí)際需要修改了一些函數(shù)
基本函數(shù)如下:
/// <summary>
/// 需要分頁(yè)時(shí)使用,根據(jù)參數(shù)和ConditionExpress獲取DataTable
/// </summary>
/// <param name="_tableName">表名</param>
/// <param name="_fieldNames">字段名集合,用逗號(hào)分開(kāi)</param>
/// <param name="_OrderColumn">排序字段,用于統(tǒng)計(jì)有多少條記錄</param>
/// <param name="IsDesc">是否倒序</param>
/// <param name="_indexColumn">自增字段名</param>
/// <param name="_currentPage">當(dāng)前頁(yè)</param>
/// <param name="pageSize">頁(yè)大小</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è)置分頁(yè)
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();
}
//分頁(yè)切換
protected void anPager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
this.anPager.CurrentPageIndex = e.NewPageIndex;
this.tbxType.Text = this.tbxType.Text;
bind();
}
之前一直在頁(yè)數(shù)方面直接用數(shù)字寫(xiě)進(jìn)去,沒(méi)有寫(xiě)成anPager.PageSize=10;的形式,在老湯的提醒下,做了修改,也解決了一直困擾我的問(wèn)題。
復(fù)制代碼 代碼如下:
/// <summary>
/// 需要分頁(yè)時(shí)使用,根據(jù)參數(shù)和ConditionExpress獲取DataTable
/// </summary>
/// <param name="_tableName">表名</param>
/// <param name="_fieldNames">字段名集合,用逗號(hào)分開(kāi)</param>
/// <param name="_OrderColumn">排序字段,用于統(tǒng)計(jì)有多少條記錄</param>
/// <param name="IsDesc">是否倒序</param>
/// <param name="_indexColumn">自增字段名</param>
/// <param name="_currentPage">當(dāng)前頁(yè)</param>
/// <param name="pageSize">頁(yè)大小</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)用如下:
復(fù)制代碼 代碼如下:
private void bind()
{
int rowCount = 1;
string wherestr = string.Empty;
//設(shè)置分頁(yè)
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();
}
復(fù)制代碼 代碼如下:
//分頁(yè)切換
protected void anPager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
this.anPager.CurrentPageIndex = e.NewPageIndex;
this.tbxType.Text = this.tbxType.Text;
bind();
}
之前一直在頁(yè)數(shù)方面直接用數(shù)字寫(xiě)進(jìn)去,沒(méi)有寫(xiě)成anPager.PageSize=10;的形式,在老湯的提醒下,做了修改,也解決了一直困擾我的問(wèn)題。
您可能感興趣的文章:
- 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訪問(wèn)數(shù)量統(tǒng)計(jì)代碼
- asp.net中調(diào)用Office來(lái)制作3D統(tǒng)計(jì)圖的實(shí)例代碼
- ASP.net中網(wǎng)站訪問(wèn)量統(tǒng)計(jì)方法代碼
- php模仿asp Application對(duì)象在線人數(shù)統(tǒng)計(jì)實(shí)現(xiàn)方法
- 四步完成asp網(wǎng)頁(yè)設(shè)計(jì)流量統(tǒng)計(jì)
相關(guān)文章
用Html5與Asp.net MVC上傳多個(gè)文件的實(shí)現(xiàn)代碼
Html 5 的有一些File API,對(duì)Form表單增強(qiáng)的特性,讓我們輕松支持多文件上傳,看下面的Html片斷代碼2012-08-08
.net中自定義錯(cuò)誤頁(yè)面的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于.net中自定義錯(cuò)誤頁(yè)面實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06
asp net core 2.1中如何使用jwt(從原理到精通)
這篇文章主要給大家介紹了關(guān)于asp net core 2.1中如何使用jwt(從原理到精通)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2018-11-11
ASP.NET?Core使用MiniProfiler分析應(yīng)用
這篇文章介紹了ASP.NET?Core使用MiniProfiler分析應(yīng)用的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02
VS2015自帶LocalDB數(shù)據(jù)庫(kù)用法詳解
這篇文章主要為大家詳細(xì)介紹了VS2015自帶LocalDB數(shù)據(jù)庫(kù)的用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
.NET 下運(yùn)用策略模式(組合行為和實(shí)體的一種模式)
我簡(jiǎn)單的理解策略模式就是把行為(方法)單獨(dú)的抽象出來(lái),并采用組合(Has-a)的方式,來(lái)組合行為和實(shí)體的一種模式比如,.NET中對(duì)數(shù)組排序的Sort的方法就是一個(gè)策略模式的實(shí)現(xiàn)模板2012-12-12

