概述
与其讲是分页控件 不如说是一个分页类。
在网上搜集的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 文件
在使用页面中引入即可
看一下分页效果。
和博客园的分页效果几乎一致
使用方法
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}";
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 名称
完整类代码
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();
}
}
}
写的很粗糙。。。
demo下载
转载于:https://www.cnblogs.com/tandly/archive/2010/01/04/1639157.html
最后
以上就是欣慰美女为你收集整理的发一个仿博客园的分页控件的全部内容,希望文章能够帮你解决发一个仿博客园的分页控件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复