实例分享:自己开发的自定义分页控件

发布时间:2007年04月24日      浏览次数:2926 次
一直以来都觉得分页是麻烦的事情,.NET中虽然有DATAGRID的分页,但用其他形式的列表仍然需要使用到分页,一次一次的写不利于效率及面向对象的方法,用类或用户控件也总觉得怪怪的,用第3方的自己觉得不放心,也不利于自己进行修改,干脆就自己写了一个。

  (另外注意:在控件编译时,可以在AssemblyInfo.cs文件中设置控件的标签和名称空间,如:
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Web.UI;
[assembly: TagPrefix("PublicControls","PublicControls")] // 自定义控件前缀

  第一个参数是名称空间(必须是你的控件类的名称空间),第二个是标签名(可自定义)

  记得要加入System.Web.UI;名称空间,另外 将控件类内的

[DefaultProperty("Text"),
ToolboxData("<PublicControls:PageNavigation runat=server></PublicControls:PageNavigation>")]这句屏蔽掉
所有代码如下:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
namespace PublicControls
{
///<summary>
/// PageNavigation 分页导航控件。
///</summary>
[DefaultProperty("Text"),
ToolboxData("<PublicControls:PageNavigation runat=server></PublicControls:PageNavigation>")]
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;
///<summary>
///委托页面索引事件
///</summary>
public event EventHandler PageIndexChange;
#endregion
#region属性

///<summary>
///相关属性样式字符串属性
///</summary>
[Bindable(false),
Category("StyleString"),
DefaultValue("Align=left")]
public string StyleString
{
get
{
_style = (string)ViewState["StyleString"];
return _style;
}
set
{
_style= value;
ViewState["StyleString"] = _style;
}
}
///<summary>
///记录总数
///</summary>
[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;

}
}
///<summary>
///页面大小
///</summary>
[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;

}
}
///<summary>
///当前页索引
///</summary>
[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;
}
}
///<summary>
///页群属性
///</summary>
public int Pages
{
get
{
if (this.IsNumeric(ViewState["Pages"]))
{
_pages = (int)ViewState["Pages"];
}
return _pages;
}
set
{
_pages = value;
ViewState["Pages"] = _pages;
}
}
///<summary>
///当前页群索引
///</summary>
public int CurrentPages
{
get
{
if (this.IsNumeric(ViewState["CurrentPages"]))
{
_currentpages = (int)ViewState["CurrentPages"];
}
return _currentpages;
}
set
{
_currentpages = value;
ViewState["CurrentPages"] = _currentpages;
}
}
///<summary>
///标记样式
///</summary>
[Bindable(false),
Category("first"),
DefaultValue("第一页")]
public string First
{
get
{
_first = (string)ViewState["First"];
return _first;
}
set
{
_first = value;
ViewState["First"] = _first;
}
}
///<summary>
///标记样式
///</summary>
[Bindable(false),
Category("pres"),
DefaultValue("前N页")]
public string Pres
{
get
{
_pres = (string)ViewState["Pres"];
return _pres;
}
set
{
_pres = value;
ViewState["Pres"] = _pres;
}
}
///<summary>
///标记样式
///</summary>
[Bindable(false),
Category("pre"),
DefaultValue("前一页")]
public string Pre
{
get
{
_pre = (string)ViewState["Pre"];
return _pre;
}
set
{
_pre = value;
ViewState["Pre"] = _pre;
}
}
///<summary>
///标记样式
///</summary>
[Bindable(false),
Category("next"),
DefaultValue("后一页")]
public string Next
{
get
{
_next = (string)ViewState["Next"];
return _next;
}
set
{
_next = value;
ViewState["Next"] = _next;
}
}
///<summary>
///标记样式
///</summary>
[Bindable(false),
Category("nexts"),
DefaultValue("后N页")]
public string Nexts
{
get
{
_nexts = (string)ViewState["Nexts"];
return _nexts;
}
set
{
_nexts = value;
ViewState["Nexts"] = _nexts;
}
}
///<summary>
///标记样式
///</summary>
[Bindable(false),
Category("last"),
DefaultValue("最后一页")]
public string Last
{
get
{
_last = (string)ViewState["Last"];
return _last;
}
set
{
_last = value;
ViewState["Last"] = _last;
}
}
#endregion
#region事件
///<summary>
///当由类实现时,使服务器控件能够处理将窗体发送到服务器时引发的事件。
///</summary>
///<param name="e">所传递的参数</param>
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<CurrentPages*Pages && Pageindex>0)
{
CurrentPages--;
}
}
}
else if(e=="next")
{
if(Pageindex<(Count+Pagesize-1)/Pagesize -1)
{
Pageindex++;
if(Pageindex>(CurrentPages+1)*Pages-1 && Pageindex<Count-1)
{
CurrentPages++;
}
}
}
else if(e=="pres")
{
Pageindex -= Pages;
if(Pageindex<0)
{
Pageindex = 0;
}
if(Pageindex<CurrentPages*Pages && Pageindex>0)
{
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<Count-1)
{
CurrentPages++;
}
}
else
{
Pageindex = int.Parse(e.ToString());
}

//发生回送事件时引发OnPageIndexChange事件
OnPageIndexChange(System.EventArgs.Empty);
}
///<summary>
///当由类实现时,为 ASP.NET 服务器控件处理回发数据。
///</summary>
///<param name="postDataKey">数据集合元素索引</param>
///<param name="values">string 的排序集合</param>
///<returns></returns>
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;
}
///<summary>
///当由类实现时,用信号要求服务器控件对象通知 ASP.NET 应用程序该控件的状态已更改。
///</summary>
public void RaisePostDataChangedEvent()
{
}
///<summary>
///当页面索引改变,触发委托
///</summary>
///<param name="e"></param>
protected void OnPageIndexChange(System.EventArgs e)
{
//委托给加载控件页的PageIndexChange事件
if (PageIndexChange!=null)
{
PageIndexChange(this,e);
}
}
///<summary>
///预呈现
///</summary>
///<param name="e"></param>
protected override void OnPreRender(EventArgs e)
{
}
#endregion
#region输出
///<summary>
///将此控件呈现给指定的输出参数。
///</summary>
///<param name="output">要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter output)
{
string Pagestring = ""; //定义页码呈现字符串
string Text = null; //输出主结构字符串变量定义
int NO;
//输出主结构
Text = "<div $style$>";

Text +="<a href =\"javascript:"+ Page.GetPostBackEventReference(this,"top")+ "\">"+First+"</a> ";
Text +="<a href =\"javascript:"+ Page.GetPostBackEventReference(this,"pres")+ "\">"+Pres+"</a> ";
Text +="<a href =\"javascript:"+ Page.GetPostBackEventReference(this,"pre")+ "\">"+Pre+"</a> ";
Text +="$Pagestring$";
Text +="<a href =\"javascript:"+ Page.GetPostBackEventReference(this,"next")+ "\">"+Next+"</a> ";
Text +="<a href =\"javascript:"+ Page.GetPostBackEventReference(this,"nexts")+ "\">"+Nexts+"</a> ";

Text +="<a href =\"javascript:"+ Page.GetPostBackEventReference(this,"last")+ "\">"+Last+"</a> ";
Text +="</div>";//<input type = hidden name ="+this.UniqueID+" value = "+this.Pageindex+"|"+this.CurrentPages+">";
//当页大小小于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 += "<a href = \"javascript:"+ Page.GetPostBackEventReference(this,NO.ToString())+ "\">"+(NO+1).ToString()+"</a> ";
}
//如果不是,页面部分无连接
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其他函数
///<summary>
///判断是否为数字字符串
///</summary>
///<param name="str">需验证的字符串</param>
///<returns>判断结果,符合条件为True,不符合条件为False</returns>
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;
}
///<summary>
///判断是否为数字字符串
///</summary>
///<param name="obj">需验证的字符串</param>
///<returns>判断结果,符合条件为True,不符合条件为False</returns>
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
}
}
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!