C#實現(xiàn)分頁組件的方法
分頁無論是前端和后端,基本都有廣泛應(yīng)用!下面通過一個小小案例完成這個分頁效果:
參數(shù)含義:
string urlFormat: 要傳給服務(wù)器端的URL地址格式,方便在點超鏈接時進(jìn)行相應(yīng)的跳轉(zhuǎn)
long totalSize: 總的數(shù)據(jù)條數(shù)。
long pageSize: 每頁多少條數(shù)據(jù)
long currentPage: 當(dāng)前的頁數(shù)
后面通過具體的一個案例來用這個分頁方法:
一.分頁方法:
/// <summary>
/// 生成頁碼的html
/// </summary>
/// <param name="urlFormat">超鏈接的格式。list.ashx?pagenum={pageNum}。地址中用{pagenum}做為當(dāng)前頁碼的占位符</param></param>
/// <param name="totalSize">總數(shù)據(jù)條數(shù)</param>
/// <param name="pageSize">每頁多少條數(shù)據(jù)</param>
/// <param name="currentPage">當(dāng)前頁</param>
/// <returns></returns>
public static RawString Pager(string urlFormat, long totalSize,
long pageSize, long currentPage)
{
StringBuilder sb = new StringBuilder();
//總頁數(shù)
long totalPageCount = (long)Math.Ceiling((totalSize * 1.0f) / (pageSize * 1.0f));
//當(dāng)前頁的前幾頁
long firstPage = Math.Max(currentPage - 5, 1);
//當(dāng)前頁的后幾頁
long lastPage = Math.Min(currentPage + 6, totalPageCount);
//繪制分頁,首頁
sb.AppendLine("<div><a href='" + urlFormat.Replace("{pageNum}", "1") + "'>首頁</a>");
//繪制分頁中間數(shù)據(jù)部分
for (long i = firstPage; i < lastPage; i++)
{
string url = urlFormat.Replace("{pageNum}", i.ToString());
if (i == currentPage) //點擊后就不顯示超鏈接
{
sb.AppendLine("<a>" + i + "</a>");
}
else
{
sb.AppendLine("<a href='" + url + "'>" + i + "</a>");
}
}
//顯示最后一頁
sb.AppendLine("<a href='" + urlFormat.Replace("{pageNum}", totalPageCount.ToString()) + "'>末頁</a></div>");
return new RawString(sb.ToString());
}
二.案例調(diào)用:
服務(wù)器端(test.ashx):這里為了方便看到效果,展示數(shù)據(jù)直接用的固定數(shù)據(jù)
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
long pn = Convert.ToInt64(context.Request["pn"]);
if (pn == 0) //Convert.ToInt64(null)返回的是0
{
pn = 1;
}
long[] num = new long[50]; //這里的數(shù)據(jù)用的是固定數(shù)據(jù)
for (int i = 0; i < 50; i++)
{
num[i] = ((pn-1) * 50) + i;
}
OutputRazor(context, "~/test.cshtml", new { nums=num,page=pn}); //這里用的Razor模板引擎
}
這里的Razor方法見:Razor模板引擎簡單介紹
UI端展示(test.cshtml):
<body>
<ul>
@{
foreach (int i in Model.nums)
{
<li>@i</li>
}
}
</ul>
@Pager("test.ashx?pn={pageNum}", 1020, 50, Model.page);
</body>
效果圖:

三.jQuery分頁插件:
前面寫的這些主要是進(jìn)行功能的實現(xiàn),樣式效果差了點。下面貼上通過jQuery實現(xiàn)的分頁效果
jQuery的效果圖,及調(diào)用方法:

完整代碼:
<!DOCTYPE html>
<html lang="zh-cn" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>一個非常簡單的jQuery分頁插件</title>
<style>
*{ margin:0; padding:0; list-style:none;}
a{ text-decoration:none;}
a:hover{ text-decoration:none;}
.tcdPageCode{padding: 15px 20px;text-align: left;color: #ccc;}
.tcdPageCode a{display: inline-block;color: #428bca;display: inline-block;height: 25px; line-height: 25px; padding: 0 10px;border: 1px solid #ddd; margin: 0 2px;border-radius: 4px;vertical-align: middle;}
.tcdPageCode a:hover{text-decoration: none;border: 1px solid #428bca;}
.tcdPageCode span.current{display: inline-block;height: 25px;line-height: 25px;padding: 0 10px;margin: 0 2px;color: #fff;background-color: #428bca; border: 1px solid #428bca;border-radius: 4px;vertical-align: middle;}
.tcdPageCode span.disabled{ display: inline-block;height: 25px;line-height: 25px;padding: 0 10px;margin: 0 2px; color: #bfbfbf;background: #f2f2f2;border: 1px solid #bfbfbf;border-radius: 4px;vertical-align: middle;}
</style>
</head>
<body>
<!-- 代碼部分begin -->
<div class="tcdPageCode">
</div>
<pre>
調(diào)用方法:
$(".tcdPageCode").createPage({
pageCount:20,
current:1,
backFn:function(p){
//單擊回調(diào)方法,p是當(dāng)前頁碼
}
});
pageCount:總頁數(shù)
current:當(dāng)前頁
</pre>
</body>
<script src="/ajaxjs/jquery.min.js"></script>
<script src="/ajaxjs/jquery.page.js"></script>
<script>
$(".tcdPageCode").createPage({
pageCount:20,
current:5,
backFn:function(p){
console.log(p);
}
});
</script>
<!-- 代碼部分end -->
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#針對xml文件轉(zhuǎn)化Dictionary的方法
這篇文章主要介紹了C#針對xml文件轉(zhuǎn)化Dictionary的方法,是C#操作XML文件的典型應(yīng)用,具有一定參考借鑒價值,需要的朋友可以參考下2015-01-01
C#中Winfrom默認(rèn)輸入法的設(shè)置方法
這篇文章主要介紹了C#中Winfrom默認(rèn)輸入法的設(shè)置方法,以實例形式較為詳細(xì)的分析了C#中輸入法設(shè)置的相關(guān)技巧,需要的朋友可以參考下2015-05-05
C#如何在窗體程序中操作數(shù)據(jù)庫數(shù)據(jù)
這篇文章主要介紹了C#如何在窗體程序中操作數(shù)據(jù)庫數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04

