开发的自定义分页控件(转)


(另外注意:在控件编译时,可以在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=""
{
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=""
{
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>&nbsp;";

Text +="<a href =/"javascript:"+ Page.GetPostBackEventReference(this="","pres")+ "/">"+Pres+"</a>&nbsp;";

Text +="<a href =/"javascript:"+ Page.GetPostBackEventReference(this="","pre")+ "/">"+Pre+"</a>&nbsp;";

Text +="$Pagestring$";

Text +="<a href =/"javascript:"+ Page.GetPostBackEventReference(this="","next")+ "/">"+Next+"</a>&nbsp;";

Text +="<a href =/"javascript:"+ Page.GetPostBackEventReference(this="","nexts")+ "/">"+Nexts+"</a>&nbsp;";

Text +="<a href =/"javascript:"+ Page.GetPostBackEventReference(this="","last")+ "/">"+Last+"</a>&nbsp;";

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>&nbsp;";
}
//如果不是,页面部分无连接
else
{
Pagestring += (NO+1).ToString()+"&nbsp;";
}
}
}

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
}
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值