using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
namespace PublicControls
{
///
/// PageNavigation 分页导航控件。
/// [DefaultProperty("Text"),
ToolboxData("
")]
public class PageNavigation : Control,IPostBackDataHandler,IPostBackEventHandler
{
#region预定义
private string _style;
private int _count;
private int _pagesize;
private int _pageindex;
private int _pages; //页群
private int _currentpages;
private string _first;
private string _pres;
private string _pre;
private string _next;
private string _nexts;
private string _last;
///
///委托页面索引事件
/// public event EventHandler PageIndexChange;
#endregion
#region属性
///
///相关属性样式字符串属性
/// [Bindable(false),
Category("StyleString"),
DefaultValue("Align=left")]
public string StyleString
{
get
{
_style = (string)ViewState["StyleString"];
return _style;
}
set
{
_style= value;
ViewState["StyleString"] = _style;
}
}
///
///记录总数
/// [Bindable(false),
Category("Count"),
DefaultValue(0)]
public int Count
{
get
{
if (this.IsNumeric(ViewState["Count"]))
{
_count = (int)ViewState["Count"];
}
return _count;
}
set
{
_count = value;
ViewState["Count"] = _count;
}
}
///
///页面大小
/// [Bindable(false),
Category("Pagesize"),
DefaultValue(10)]
public int Pagesize
{
get
{
if (this.IsNumeric(ViewState["Pagesize"]))
{
_pagesize = (int)ViewState["Pagesize"];
}
return _pagesize;
}
set
{
_pagesize = value;
ViewState["Pagesize"] = _pagesize;
}
}
///
///当前页索引
/// [Bindable(false),
Category("Pageindex"),
DefaultValue(0)]
public int Pageindex
{
get
{
if (this.IsNumeric(ViewState["Pageindex"]))
{
_pageindex = (int)ViewState["Pageindex"];
}
return _pageindex;
}
set
{
_pageindex = value;
ViewState["Pageindex"] = _pageindex;
}
}
///
///页群属性
/// public int Pages
{
get
{
if (this.IsNumeric(ViewState["Pages"]))
{
_pages = (int)ViewState["Pages"];
}
return _pages;
}
set
{
_pages = value;
ViewState["Pages"] = _pages;
}
}
///
///当前页群索引
/// public int CurrentPages
{
get
{
if (this.IsNumeric(ViewState["CurrentPages"]))
{
_currentpages = (int)ViewState["CurrentPages"];
}
return _currentpages;
}
set
{
_currentpages = value;
ViewState["CurrentPages"] = _currentpages;
}
}
///
///标记样式
/// [Bindable(false),
Category("first"),
DefaultValue("第一页")]
public string First
{
get
{
_first = (string)ViewState["First"];
return _first;
}
set
{
_first = value;
ViewState["First"] = _first;
}
}
///
///标记样式
/// [Bindable(false),
Category("pres"),
DefaultValue("前N页")]
public string Pres
{
get
{
_pres = (string)ViewState["Pres"];
return _pres;
}
set
{
_pres = value;
ViewState["Pres"] = _pres;
}
}
///
///标记样式
/// [Bindable(false),
Category("pre"),
DefaultValue("前一页")]
public string Pre
{
get
{
_pre = (string)ViewState["Pre"];
return _pre;
}
set
{
_pre = value;
ViewState["Pre"] = _pre;
}
}
///
///标记样式
/// [Bindable(false),
Category("next"),
DefaultValue("后一页")]
public string Next
{
get
{
_next = (string)ViewState["Next"];
return _next;
}
set
{
_next = value;
ViewState["Next"] = _next;
}
}
///
///标记样式
/// [Bindable(false),
Category("nexts"),
DefaultValue("后N页")]
public string Nexts
{
get
{
_nexts = (string)ViewState["Nexts"];
return _nexts;
}
set
{
_nexts = value;
ViewState["Nexts"] = _nexts;
}
}
///
///标记样式
/// [Bindable(false),
Category("last"),
DefaultValue("最后一页")]
public string Last
{
get
{
_last = (string)ViewState["Last"];
return _last;
}
set
{
_last = value;
ViewState["Last"] = _last;
}
}
#endregion
#region事件
///
///当由类实现时,使服务器控件能够处理将窗体发送到服务器时引发的事件。
/// ///
所传递的参数
public void RaisePostBackEvent(string e)
{
//当发生回送的时候改变控件当前索引
if(e=="top")
{
Pageindex = 0;
CurrentPages = 0 ;
}
else if(e=="last")
{
Pageindex = (Count+Pagesize-1)/Pagesize -1;
CurrentPages = ((Count+Pagesize-1)/Pagesize+Pages-1)/ Pages -1;
if(Pageindex<0)
{
Pageindex=0;
}
if(CurrentPages<0)
{
CurrentPages=0;
}
}
else if(e=="pre")
{
if(Pageindex>0)
{
Pageindex--;
if(Pageindex
0)
{
CurrentPages--;
}
}
}
else if(e=="next")
{
if(Pageindex<(Count+Pagesize-1)/Pagesize -1)
{
Pageindex++;
if(Pageindex>(CurrentPages+1)*Pages-1 && Pageindex {
CurrentPages++;
}
}
}
else if(e=="pres")
{
Pageindex -= Pages;
if(Pageindex<0)
{
Pageindex = 0;
}
if(Pageindex0)
{
CurrentPages--;
}
}
else if(e=="nexts")
{
Pageindex += Pages;
if(Pageindex>(Count+Pagesize-1)/Pagesize -1)
{
Pageindex = (Count+Pagesize-1)/Pagesize -1;
}
if(Pageindex>(CurrentPages+1)*Pages-1 && Pageindex {
CurrentPages++;
}
}
else
{
Pageindex = int.Parse(e.ToString());
}
//发生回送事件时引发OnPageIndexChange事件
OnPageIndexChange(System.EventArgs.Empty);
}
///
///当由类实现时,为 ASP.NET 服务器控件处理回发数据。
///
///数据集合元素索引
///string 的排序集合
///
public bool LoadPostData(string postDataKey, NameValueCollection values)
{
//Pageindex = Int32.Parse(values[this.UniqueID].Split('|')[0]);
//CurrentPages = Int32.Parse(values[this.UniqueID].Split('|')[1]);
return false;
}
///
///当由类实现时,用信号要求服务器控件对象通知 ASP.NET 应用程序该控件的状态已更改。
///
public void RaisePostDataChangedEvent()
{
}
///
///当页面索引改变,触发委托
///
///
protected void OnPageIndexChange(System.EventArgs e)
{
//委托给加载控件页的PageIndexChange事件
if (PageIndexChange!=null)
{
PageIndexChange(this,e);
}
}
///
///预呈现
///
///
protected override void OnPreRender(EventArgs e)
{
}
#endregion
#region输出
///
///将此控件呈现给指定的输出参数。
///
///要写出到的 HTML 编写器
protected override void Render(HtmlTextWriter output)
{
string Pagestring = ""; //定义页码呈现字符串
string Text = null; //输出主结构字符串变量定义
int NO;
//输出主结构
Text = "";//";
//当页大小小于0时,还原为1,防止预算出现除0错误
if (!(Pagesize>0))
{
Pagesize = 1;
}
//计算出总页数,并循环输出页码
for (int i = 0;i< Pages ;i++ )
{
//获得页群内页码
NO = Pages * CurrentPages + i;
if(NO<(Count+Pagesize-1)/Pagesize)
{
//判断页码是否为当前页
if (Pageindex != NO)
{
Pagestring += ""+(NO+1).ToString()+" ";
}
//如果不是,页面部分无连接
else
{
Pagestring += (NO+1).ToString()+" ";
}
}
}
if (!(Pagestring.Trim().Length>0))
{
Pagestring = "1";
}
//替换对齐属性
Text = Text.Replace("style",StyleString);
//替换页码部分
Text = Text.Replace("Pagestring",Pagestring);
output.Write(Text);
}
#endregion
#region其他函数
///
///判断是否为数字字符串
///
///需验证的字符串
///判断结果,符合条件为True,不符合条件为False
public bool IsNumeric(string str)
{
//判断是否为空
if (str == null || str.Length==0)
{
return false;
}
//循环检查每个字符
foreach(char c in str)
{
if (!Char.IsNumber(c))
{
return false;
}
}
return true;
}
///
///判断是否为数字字符串
///
///需验证的字符串
///判断结果,符合条件为True,不符合条件为False
public bool IsNumeric(object obj)
{
obj += "";
string str = obj.ToString();
//判断是否为空
if (str == null || str.Length==0)
{
return false;
}
//循环检查每个字符
foreach(char c in str)
{
if (!Char.IsNumber(c))
{
return false;
}
}
return true;
}
#endregion
}
}