祝贺一下~好不容易把分页做出来了之前都是用GridView自带的分页版式难看不说还极不优化一次取出所有记录然后进行假分页
现在用aspNetPager控件做出的真分页就好多了不过还有改进的地方SQL语句如果换成存储过程效率会更高
首先在SqlHelpercs(DAL层中的数据库助手类用于写可以复用的基本增删查改方法)中加上以下代码
Code
/**//// <summary>
/// 获取分页数据
/// </summary>
/// <param name=sql>sql语句</param>
/// <param name=currentPage>当前页</param>
/// <param name=pagesize>每页显示数</param>
/// <param name=recordcount></param>
/// <returns></returns>
public static DataSet GetPage(string sql int currentPage int pagesize out int recordcount)
{
openCon();
sqlDsClear();
sqlDa = new SqlDataAdapter(sql sqlConn);
int startRow = (currentPage ) * pagesize;
sqlDaFill(sqlDs startRow pagesize table);
recordcount = GetPageRecord(sql);
closeCon();
return sqlDs;
}
//返回总的记录数
public static int GetPageRecord(string sql)
{
openCon();
sql = RegexReplace(sql order by* );
sql = select count(*) from ( + sql + ) as temp;
sqlCmd = new SqlCommand(sql sqlConn);
int recordcount = intParse(sqlCmdExecuteScalar()ToString());
closeCon();
return recordcount;
}
然后在BLL层新建一个PageManagercs的分页操作类封装一下DAL层方法
Code
/**//// <summary>
/// 获取分页数据
/// </summary>
/// <param name=sql>sql语句</param>
/// <param name=currentPage>当前页</param>
/// <param name=pagesize>每页显示数</param>
/// <param name=recordcount></param>
/// <returns></returns>
public static DataSet GetPage(string sql int currentPage int pagesize out int recordcount)
{
return SQLHelperGetPage(sql currentPage pagesize out recordcount);
}
需分页的数据绑定处是这样的
Code
<asp:Repeater ID=repNewsList runat=server>
<ItemTemplate>
<tr>
<td align=center><a listaspx?caid=<%# Eval(caId) %>><%# Eval(name) %></a></td>
<td align=center><%# Eval(createTime) %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
然后在绑定代码下方加上分页控件(当然这个可以随便放怎么好看怎么放)
Code
<!分页控件>
<div >
<webdiyer:AspNetPager ID=AspNetPager runat=server AlwaysShow=True UrlPaging=true
FirstPageText=首页 LastPageText=末页 NextPageText=下一页 NumericButtonCount=
onpagechanged=AspNetPager_PageChanged PagingButtonSpacing=px NumericButtonTextFormatString=[{}]
PRevPageText=上一页 SubmitButtonText=Go TextAfterPageIndexBox=页
TextBeforePageIndexBox=转到 ShowCustomInfoSection=Left
CustomInfoHTML=目前是第%CurrentPageIndex%页 / 总共%PageCount%页>
</webdiyer:AspNetPager>
</div>
最后在aspxcs中加上数据的分页绑定方法(这里的SQL语句要根据列表显示的需要进行调整)
Code
/**//// <summary>
/// 绑定带有分页的新闻列表
/// </summary>
public void BindRepeater()
{
int caid = intParse(RequestQueryString[caid]);
string Sql = select * from news where caId= + caid + order by createTime desc;
int CurrentPage = AspNetPagerCurrentPageIndex;
int PageSize = AspNetPagerPageSize;
int RecordCount;
DataSet ds = PageManagerGetPage(Sql CurrentPage PageSize out RecordCount);
AspNetPagerRecordCount = RecordCount;
AspNetPagerCustomInfoHTML += 共 + RecordCount + 条新闻</b>;
repNewsListDataSource = ds;
repNewsListDataBind();
}
别忘了在page_load调用一下
protected void Page_Load(object sender EventArgs e)
{
if (!PageIsPostBack)
{
BindRepeater();
}
}
还有分页控件的PageChanged事件里也调用一下
protected void AspNetPager_PageChanged(object sender EventArgs e)
{
BindRepeater();
}
最后再补充一个非常漂亮的翻页样式清爽超酷型~
Code
<style>
anpager
{}{
fontsize:px;
}
anpager cpb
{}{
background:#FA none repeat scroll ;
border:px solid #CCCCCC;
color:#FFFFFF;
fontweight:bold;
margin:px px ;
padding:px px ;
}
anpager a
{}{
background:#FFFFFF none repeat scroll ;
border:px solid #CCCCCC;
color:#FA;
margin:px px ;
padding:px px ;
textdecoration:none
}
anpager a:hover
{}{
background:#FA none repeat scroll ;
border:px solid #FA;
color:#FFFFFF;
}
</style>
然后在AspNetPager中加上以下四个属性搞定!
CSSClass=anpager
CurrentPageButtonClass=cpb
CustomInfoClass=
CustomInfoTextAlign=Left