轉(zhuǎn)帖|其它|編輯:郝浩|2010-10-27 14:06:22.000|閱讀 843 次
概述:之前都是用GridView自帶的分頁(yè),版式難看不說(shuō),還極不優(yōu)化,一次取出所有記錄,然后進(jìn)行假分頁(yè)?,F(xiàn)在用aspNetPager控件做出的真分頁(yè),就好多了,不過(guò)還有改進(jìn)的地方,SQL語(yǔ)句如果換成存儲(chǔ)過(guò)程效率會(huì)更高。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
之前都是用GridView自帶的分頁(yè),版式難看不說(shuō),還極不優(yōu)化,一次取出所有記錄,然后進(jìn)行假分頁(yè)。
現(xiàn)在用aspNetPager控件做出的真分頁(yè),就好多了,不過(guò)還有改進(jìn)的地方,SQL語(yǔ)句如果換成存儲(chǔ)過(guò)程效率會(huì)更高。
首先在SqlHelper.cs(DAL層中的數(shù)據(jù)庫(kù)助手類(lèi),用于寫(xiě)可以復(fù)用的基本增刪查改方法)中加上以下代碼:
Code
/**//// <summary>
/// 獲取分頁(yè)數(shù)據(jù)
/// </summary>
/// <param name="sql">sql語(yǔ)句</param>
/// <param name="currentPage">當(dāng)前頁(yè)</param>
/// <param name="pagesize">每頁(yè)顯示數(shù)</param>
/// <param name="recordcount"></param>
/// <returns></returns>
public static DataSet GetPage(string sql, int currentPage, int pagesize, out int recordcount)
{
openCon();
sqlDs.Clear();
sqlDa = new SqlDataAdapter(sql, sqlConn);
int startRow = (currentPage - 1) * pagesize;
sqlDa.Fill(sqlDs, startRow, pagesize, "table");
recordcount = GetPageRecord(sql);
closeCon();
return sqlDs;
}
//返回總的記錄數(shù)
public static int GetPageRecord(string sql)
{
openCon();
sql = Regex.Replace(sql, "order by.*", "");
sql = "select count(*) from (" + sql + ") as temp";
sqlCmd = new SqlCommand(sql, sqlConn);
int recordcount = int.Parse(sqlCmd.ExecuteScalar().ToString());
closeCon();
return recordcount;
}
然后在BLL層新建一個(gè)PageManager.cs的分頁(yè)操作類(lèi),封裝一下DAL層方法:
Code
/**//// <summary>
/// 獲取分頁(yè)數(shù)據(jù)
/// </summary>
/// <param name="sql">sql語(yǔ)句</param>
/// <param name="currentPage">當(dāng)前頁(yè)</param>
/// <param name="pagesize">每頁(yè)顯示數(shù)</param>
/// <param name="recordcount"></param>
/// <returns></returns>
public static DataSet GetPage(string sql, int currentPage, int pagesize, out int recordcount)
{
return SQLHelper.GetPage(sql, currentPage, pagesize, out recordcount);
}
asp.net需分頁(yè)的數(shù)據(jù)綁定處是這樣的:
Code
<asp:Repeater ID="repNewsList" runat="server">
<ItemTemplate>
<tr>
<td align="center"><a href="list.aspx?caid=<%# Eval("caId") %>"><%# Eval("name") %></a></td>
<td align="center"><%# Eval("createTime") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
然后在綁定代碼下方加上分頁(yè)控件(當(dāng)然這個(gè)可以隨便放,怎么好看怎么放):
Code
<!--分頁(yè)控件-->
<div style="text-align:center; height:50px; line-height:50px;">
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="True" UrlPaging="true"
FirstPageText="金喜正規(guī)買(mǎi)球" LastPageText="末頁(yè)" NextPageText="下一頁(yè)" NumericButtonCount="5"
onpagechanged="AspNetPager1_PageChanged" PagingButtonSpacing="10px" NumericButtonTextFormatString="[{0}]"
PRevPageText="上一頁(yè)" SubmitButtonText="Go" TextAfterPageIndexBox="頁(yè)"
TextBeforePageIndexBox="轉(zhuǎn)到" ShowCustomInfoSection="Left"
CustomInfoHTML="目前是第%CurrentPageIndex%頁(yè) / 總共%PageCount%頁(yè)">
</webdiyer:AspNetPager>
</div>
最后在aspx.cs中加上數(shù)據(jù)的分頁(yè)綁定方法(這里的SQL語(yǔ)句要根據(jù)列表顯示的需要進(jìn)行調(diào)整):
Code
/**//// <summary>
/// 綁定帶有分頁(yè)的新聞列表
/// </summary>
public void BindRepeater()
{
int caid = int.Parse(Request.QueryString["caid"]);
string Sql = "select * from news where caId=" + caid + " order by createTime desc";
int CurrentPage = AspNetPager1.CurrentPageIndex;
int PageSize = AspNetPager1.PageSize;
int RecordCount;
DataSet ds = PageManager.GetPage(Sql, CurrentPage, PageSize, out RecordCount);
AspNetPager1.RecordCount = RecordCount;
AspNetPager1.CustomInfoHTML += " 共" + RecordCount + "條新聞</b>";
repNewsList.DataSource = ds;
repNewsList.DataBind();
}
別忘了,在page_load調(diào)用一下:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindRepeater();
}
}
還有分頁(yè)控件的PageChanged事件里也調(diào)用一下:
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
BindRepeater();
}
最后再補(bǔ)充一個(gè)非常漂亮的翻頁(yè)樣式,清爽超酷型~:
Code
<style>
.anpager
{}{
font-size:12px;
}
.anpager .cpb
{}{
background:#1F3A87 none repeat scroll 0 0;
border:1px solid #CCCCCC;
color:#FFFFFF;
font-weight:bold;
margin:5px 4px 0 0;
padding:4px 5px 0;
}
.anpager a
{}{
background:#FFFFFF none repeat scroll 0 0;
border:1px solid #CCCCCC;
color:#1F3A87;
margin:5px 4px 0 0;
padding:4px 5px 0;
text-decoration:none
}
.anpager a:hover
{}{
background:#1F3A87 none repeat scroll 0 0;
border:1px solid #1F3A87;
color:#FFFFFF;
}
</style>
然后在AspNetPager中加上以下四個(gè)屬性,搞定!
CSSClass="anpager"
CurrentPageButtonClass="cpb"
CustomInfoClass=""
CustomInfoTextAlign="Left"
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:網(wǎng)絡(luò)轉(zhuǎn)載