发一个仿博客园的分页控件
与其讲是分页控件 不如说是一个分页类。
在网上搜集的CSS样式 一共24中经典样式供大家选择。
1.digg
2.yahoo
3.yahoo2
4.meneame
5.flickr
6.sabrosus
7.scott
8.quotes
9.black
10.black2
11.black-red
12.grayr
13.yellow
14.jogger
15.starcraft2
16.tres
17.megas512
18.technorati
19.youtube
20.msdn
21.badoo
22.manu
23.green-black
24.viciao
在demo 中 有css 文件
在使用页面中引入即可
看一下分页效果。
和博客园的分页效果几乎一致
使用方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using Tension.Web.Controls; using Tension.Extension; namespace PagingBarDemo { public partial class _Default : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { string pageIndex = Request.QueryString[ "pageIndex" ] ?? "1" ; int index = 1; pageIndex.IsInt32( out index); PagingBar defaultBar = new PagingBar(); defaultBar.LeftSize = 4; defaultBar.PageCount = 40; defaultBar.PageIndex = index; defaultBar.Url = "Default.aspx?pageIndex={0}" ; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | defaultBar.Target = "_black" ; Literal1.Text = defaultBar.BuildLinkHtml(); PagingBar bar2 = new PagingBar( "scott" ); bar2.LeftSize = 4; bar2.PageCount = 40; bar2.PageIndex = index; bar2.Url = "Default.aspx?pageIndex={0}" ; Literal2.Text = bar2.BuildLinkHtml(); PagingBar bar3 = new PagingBar( "green-black" ); bar3.LeftSize = 4; bar3.PageCount = 40; bar3.PageIndex = index; bar3.Url = "Default.aspx?pageIndex={0}" ; Literal3.Text = bar3.BuildLinkHtml(); PagingBar bar4 = new PagingBar( "quotes" ); bar4.LeftSize = 4; bar4.PageCount = 40; bar4.PageIndex = index; bar4.Url = "Default.aspx?pageIndex={0}" ; Literal4.Text = bar4.BuildLinkHtml(); } } } |
PagingBar defaultBar = new PagingBar();
defaultBar.LeftSize = 4; //左边显示的链接数 也就是说 当这个参数为 4 时 实际显示 9个链接 总算显示的是奇数个链接
defaultBar.PageCount = 40; //总的页数
defaultBar.PageIndex = index; //当前页
defaultBar.Url = "Default.aspx?pageIndex={0}"; //链接的URL
defaultBar.Target = "_black"; //页面在浏览器窗口中的打开方式 可以为空
Literal1.Text = defaultBar.BuildLinkHtml(); //产生HTML 输出
这里是默认构造 默认构造的话回去读取 web.config 需找配置的 CSS 样式名
需要在 appSettings 节点下添加 配置
<appSettings>
<add key="PagingBarStyle" value="yellow"/>
</appSettings>
不是默认构造的话 可以使用 一个带参构造
PagingBar bar4 = new PagingBar("quotes");
直接指定 css 名称
完整类代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text; /// <summary> /// 分页控件类 /// /// 作者:汤晓华 /// /// Email:Tandly@hotmail.com /// /// QQ:1881597 /// </summary> /// namespace Tension.Web.Controls { public class PagingBar : System.Web.UI.UserControl { string style = "" ; public PagingBar() { //样式 style = ConfigurationManager.AppSettings[ "PagingBarStyle" ]; if ( string .IsNullOrEmpty(style)) { throw new ArgumentNullException( "无法在 Web.config 文件的 AppSettings 节点下找到有关 PagingBarStyle 的配置信息。" ); } } public PagingBar( string style) { //样式 this .style = style; } //当前页 private int pageIndex; /// <summary> /// 当前页 /// </summary> public int PageIndex { get { return pageIndex; } set { pageIndex = value; } } //总页数 private int pageCount; /// <summary> /// 总页数 /// </summary> public int PageCount { get { return pageCount; } set { pageCount = value; } } //左边显示连接数 private int leftSize; /// <summary> /// 左边显示连接数 /// </summary> public int LeftSize { get { return leftSize; } set { leftSize = value; } } //连接地址 private string url; /// <summary> /// 连接地址 /// </summary> public string Url { get { return url; } set { url = value; } } //目标 private string target; /// <summary> /// 目标 /// </summary> public string Target { get { return target; } set { target = value; } } public string BuildLinkHtml() { //目标 string target = Target; //处理目前字符串 if (! string .IsNullOrEmpty(target)) { target = "target=\"" + target + "\"" ; } //需要输出的HTML StringBuilder html = new StringBuilder( "<div id=\"pagingbar\" class=\"pagingbar\"><div class=\"" ); html.Append(style); html.Append( "\">" ); #region 输出上一页 if (PageIndex <= 1) { html.Append( "<span class=\"disabled\">< Prev </span>" ); } else { html.Append( "<a href=\"" ); html.Append( string .Format(Url, PageIndex - 1)); html.Append( "\" " ); html.Append(target); html.Append( ">< Prev </a>" ); } #endregion //总长度 int barSize = LeftSize * 2 + 1; #region 输出中间 //页数小于0 if (PageCount <= 0) { return "<center><span>页数为0!!</span></center>" ; } //总页数小于正常显示的条数 则全部显示出来 if (PageCount <= barSize) { for ( int i = 1; i <= PageCount; i++) { if (PageIndex == i) { html.Append( "<span class=\"current\">" ); html.Append(i); html.Append( "</span>" ); } else { html.Append( "<a href=\"" ); html.Append( string .Format(Url, i)); html.Append( "\" " ); html.Append(target); html.Append( ">" ); html.Append(i); html.Append( "</a>" ); } } } else { if (PageIndex < leftSize + 1) { for ( int i = 1; i <= barSize; i++) { if (PageIndex == i) { html.Append( "<span class=\"current\">" ); html.Append(i); html.Append( "</span>" ); } else { html.Append( "<a href=\"" ); html.Append( string .Format(Url, i)); html.Append( "\" " ); html.Append(target); html.Append( ">" ); html.Append(i); html.Append( "</a>" ); } } html.Append( "...<a href=\"" ); html.Append( string .Format(Url, PageCount)); html.Append( "\" " ); html.Append(target); html.Append( ">" ); html.Append(PageCount); html.Append( "</a>" ); } else { html.Append( "<a href=\"" ); html.Append( string .Format(Url, 1)); html.Append( "\" " ); html.Append(target); html.Append( ">" ); html.Append(1); html.Append( "</a>..." ); if (PageIndex < PageCount - LeftSize) { for ( int i = PageIndex - leftSize; i < PageIndex; i++) { if (i == 1) { continue ; } html.Append( "<a href=\"" ); html.Append( string .Format(Url, i)); html.Append( "\" " ); html.Append(target); html.Append( ">" ); html.Append(i); html.Append( "</a>" ); } for ( int i = PageIndex; i <= PageIndex + leftSize; i++) { if (PageIndex == i) { html.Append( "<span class=\"current\">" ); html.Append(i); html.Append( "</span>" ); } else { html.Append( "<a href=\"" ); html.Append( string .Format(Url, i)); html.Append( "\" " ); html.Append(target); html.Append( ">" ); html.Append(i); html.Append( "</a>" ); } } html.Append( "...<a href=\"" ); html.Append( string .Format(Url, PageCount)); html.Append( "\" " ); html.Append(target); html.Append( ">" ); html.Append(PageCount); html.Append( "</a>" ); } else { for ( int i = PageCount - LeftSize - LeftSize; i <= PageCount; i++) { if (PageIndex == i) { html.Append( "<span class=\"current\">" ); html.Append(i); html.Append( "</span>" ); } else { html.Append( "<a href=\"" ); html.Append( string .Format(Url, i)); html.Append( "\" " ); html.Append(target); html.Append( ">" ); html.Append(i); html.Append( "</a>" ); } } } } } #endregion #region 输出下一页 if (PageIndex >= PageCount) { html.Append( "<span class=\"disabled\"> Next > </span>" ); } else { html.Append( "<a href=\"" ); html.Append( string .Format(Url, PageIndex + 1)); html.Append( "\" " ); html.Append(target); html.Append( "> Next > </a>" ); } #endregion html.Append( "</div></div>" ); return html.ToString(); } } } |
写的很粗糙。。。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述