仿Google风格的分页类

in Develop

pplive上的所有分页方式要进行变更
目前的分页是一次性显示出所有的分页
现在要进行改动

实际上很简单
只是一个起始页和终止页的计算问题

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;

/// <summary>
/// Pager 的摘要说明
/// </summary>
public class Pager
{
public Pager()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public int PageIndex
{
set
{
if (value > 0)
this._current_pageindex = value;
else
this._current_pageindex = 1;
}
get
{
return this._current_pageindex;
}
}
public int PageCount
{
get
{
return this._page_count;
}
set
{
this._page_count = value;
}
}
public string PageParam
{
set { this._page_param = value; }
}
private string _page_param;

private int _current_pageindex = 1; //当前页码
private int _page_count;
private int _per_num = 10; //当前页要显示多少页码
private int _next_num = 9; //当前页之后要显示多少页码
private string _pre_text = "上一页";
private string _next_text = "下一页";
private System.Text.StringBuilder sb = new System.Text.StringBuilder(1000);
/// <summary>
/// Pager分页类的构造
/// </summary>
/// <param name="pageindex">当前页码,从1开始</param>
/// <param name="pagecount">总页数</param>
/// <param name="param">分页参数</param>
public Pager(int pageindex, int pagecount,string param)
{
this._current_pageindex = pageindex;
this._page_count = pagecount;
this._page_param = param;
}
public string ShowPager()
{

if (_current_pageindex > 1)
sb.Append(string.Format("<a href=\"{0}\">{1}</a>", _page_param.Replace("$$",(_current_pageindex-1).ToString()), _pre_text));
int min = _current_pageindex - _per_num; //计算起始页
int max = _current_pageindex + _next_num; //计算终止页
for (int i = min; i <= max; i++)
{
if (i < 1)
continue;
if (i > _page_count)
break;
if(_current_pageindex!=i)
sb.Append(string.Format("<a href=\"{0}\">{1}</a>",_page_param.Replace("$$",i.ToString()),i));
else
sb.Append(string.Format("<span>{0}</span>", i));

}
if (_current_pageindex < _page_count)
sb.Append(string.Format("<a href=\"{0}\">{1}</a>", _page_param.Replace("$$", (_current_pageindex+1).ToString()),_next_text));
return sb.ToString();
}
}

调用
protected void Page_Load(object sender, EventArgs e)
{
int page = 1;
if (Request.QueryString["page"] != null)
page = int.Parse(Request.QueryString["page"]);
Pager pager = new Pager(page, 83,"tag,$$.html");
this._lbl_pager.Text = pager.ShowPager();
}

3 Comments

3 Comments

  1. 实际上很简单

    还是这个口头禅…[em13]

  2. NND
    一不小心这句话成口头禅了….
    [em1]

  3. 还少了一个select * from 表名…[em20]

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>