展示型文章经常会遇到分页的问题,以下是我的解决方法:
1 var pageSize = 10; 2 var page = 1; int.TryParse(Request.QueryString["page"], out page); 3 var totalNodes = ContentFloder.Count(); 4 var totalPages = (int)Math.Ceiling((double)totalNodes / pageSize); 5 if (page > totalPages) 6 { 7 page = totalPages; 8 } 9 else if (page < 1) 10 { 11 page = 1; 12 } 13 @if (totalPages > 1) 14 { 15 <div> 16 <ul class="pagination"> 17 @if (page > 1) 18 { 19 <li><a href="[email protected](page-1)">«</a></li> 20 } 21 22 @for (int p = 1; p < totalPages + 1; p++) 23 { 24 var active = (p == page) ? "active" : string.Empty; 25 <li class="@Html.Raw(active)"><a href="[email protected]" title="Go to page @p of results">@p</a></li> 26 } 27 28 @if (page < totalPages) 29 { 30 <li><a href="[email protected](page+1)">»</a></li> 31 } 32 </ul> 33 </div> 34 }
时间: 2024-10-18 04:09:09