C\#페이지 구성 요 소 를 실현 하 는 방법
매개 변수 의미:
string url Format:서버 측 에 전달 할 URL 주소 형식 으로 하이퍼링크 를 눌 렀 을 때 해당 하 는 점프 를 할 수 있 습 니 다.
long totalSize: 총 데이터 항목 수.
long pageSize: 페이지 당 몇 개의 데이터
long currentPage:현재 페이지 수
그 다음 에 구체 적 인 사례 를 통 해 이 페이지 를 나 누 는 방법 을 사용한다.
1.페이지 나 누 는 방법:
/// <summary>
/// html
/// </summary>
/// <param name="urlFormat"> 。list.ashx?pagenum={pageNum}。 {pagenum} </param></param>
/// <param name="totalSize"> </param>
/// <param name="pageSize"> </param>
/// <param name="currentPage"> </param>
/// <returns></returns>
public static RawString Pager(string urlFormat, long totalSize,
long pageSize, long currentPage)
{
StringBuilder sb = new StringBuilder();
//
long totalPageCount = (long)Math.Ceiling((totalSize * 1.0f) / (pageSize * 1.0f));
//
long firstPage = Math.Max(currentPage - 5, 1);
//
long lastPage = Math.Min(currentPage + 6, totalPageCount);
// ,
sb.AppendLine("<div><a href='" + urlFormat.Replace("{pageNum}", "1") + "'> </a>");
//
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());
}
2.사례 호출:서버 쪽(test.ashx):효 과 를 쉽게 보기 위해 데 이 터 를 직접 사용 하 는 고정 데 이 터 를 보 여 줍 니 다.
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]; //
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>
효과 그림:3.jQuery 페이지 플러그 인:
앞에서 쓴 것들 은 주로 기능 의 실현 을 진행 하 는데 스타일 효과 가 좀 떨어진다.jQuery 를 통한 페이지 효과
jQuery 의 효과 도 및 호출 방법:
전체 코드:
<!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>
:
$(".tcdPageCode").createPage({
pageCount:20,
current:1,
backFn:function(p){
// ,p
}
});
pageCount:
current:
</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>
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
WebView2를 Visual Studio 2017 Express에서 사용할 수 있을 때까지Evergreen .Net Framework SDK 4.8 VisualStudio2017에서 NuGet을 사용하기 때문에 패키지 관리 방법을 packages.config 대신 PackageReference를 사용해야...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.