一般的分页模式,都是直接用系统自带的分页。这样在每次点击另一页都会把所有的数据加载。数据很大时,严重影响页面打开效率:
该代码主要利用数据库访问技术,传该页显示的开始行和行数,读取相应的数据行。直接绑定列表。
int TotalCountRecord;// 总记录数目
int CurrentPage = 1;// 当前页数
string PageItem = "20" ;// 默认每页显示的数量
public string pxfs = "pubdate desc" ;
private void LoadSearch()
{
string tj = "" ;
DataTable dt = new DataTable ();
int startRecord = (CurrentPage - 1) * Convert .ToInt32(PageItem);
dt = CBook .GetBookInfo(tj,pxfs, startRecord, Convert .ToInt32(PageItem));
TotalCountRecord = CBook .GetbookCount(tj);
if (dt != null ||dt.Rows.Count != 0)
{
GridView1.DataSource = dt ;
GridView1.DataBind();
PageInfo.InnerHtml = "" ;
}
}
/// <summary>
/// 给列表绑定分页控制
/// </summary>
public void BuildPagers()
{
// 偏移量
int Step = 5;
int LeftNum = 0;
int RightNum = 0;
int PageCount = (int )Math .Ceiling((double )(TotalCountRecord) / Convert .ToInt32(PageItem));
if (CurrentPage - Step < 1)
LeftNum = 1;
else
LeftNum = CurrentPage - Step;
if (CurrentPage + Step > PageCount)
RightNum = PageCount;
else
RightNum = CurrentPage + Step;
string OutPut = "" ;
for (int i = LeftNum; i <= RightNum; i++)
{
if (i == CurrentPage)
OutPut += "<font color=red>" + i.ToString() +"</font>" ;
else
OutPut += "<a href='booklist.aspx'>[ " +i.ToString() + " ]</a>" ;
}
if (CurrentPage > 1)
OutPut = "<ahref='booklist.aspx? page=" + (CurrentPage - 1) + "'> 上一页</a>" + OutPut;
if (CurrentPage < PageCount)
OutPut += "<ahref='booklist.aspx? page=" + (CurrentPage + 1) + "'> 下一页</a>" ;
PageInfo.InnerHtml = OutPut;
}
( 其中:CBook 中的相关函数:
public static DataTable GetBookInfo(string tj,string px,int bs,int ps)
{
string sql = "select* from bookinfo where 1=1 " + tj;
if (px != "" )
sql += "order by " + px;
DataTable dt = CCommand .GetTable(sql,bs,ps);
return dt;
}
CCommand :中的函数
public static DataTable GetTable(string sql, int beginpage, int pagesize)
{
using (System.Data.SqlClient.SqlConnection sqlcn = new System.Data.SqlClient.SqlConnection (CCommand .conn))
{
try
{
return CDBV .ExecDataSet(sql, beginpage, pagesize);
}
catch (System.Exception ee)
{
TLssfyBase .WriteOptLog(sql+ee.Message);
return null ;
}
}
}
Cdbv 函数:
public static System.Data.DataTable ExecDataSet(string sql, int beginitem, int pagesize)
{
using (System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection (CCommand .conn))
{
SqlDataAdapter sa = new SqlDataAdapter (sql,cn);
System.Data.DataTable ds = new System.Data.DataTable ();
sa.Fill(beginitem, pagesize,ds);
return ds;
}
}
)