All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
C#中XML应用实例实例
ASP.NET安装完全手册_[Asp.Net教程]
缓存技术及在Rainbow,Portal的应用_.net资料_编程技术
获取SQL Server元数据的几种方法_[SQL Server教程]
SQL数据操作基础(初级)5_mssql学习_编程技术
.net发送邮件的一些技巧_.net资料_编程技术
target 属性怎么用 JS 来控制?_[Html教程]
ASP.NET技术获取IP与MAC地址的方法_[Asp.Net教程]
Asp.net,Ajax,学习笔记5,UpdatePanel的使用(下)_[Asp.Net教程]
MS,SQL,常用函数大全_mssql学习_编程技术
Reading number is top 10 pictures
Soldier saw beauty after the reaction
yy365网站上的美女1
Earthquake hedge common sense
两张抽象画
姑娘手慢了,已经走光了
Group of female porn in 《westwards》, uninhibited woman threatened to not the bottom line
Cesarean section, bloody, silently into it!1
China's first snake village2
Breasts woman big set 1
Summer is most suitable for young people to travel in China2
Download software ranking
Sora aoi, the maid, students' uniforms
Ashlynn Video4
都市狐狸姑娘传
Unix video tutorial12
Photoshop 8.0图象编辑软件
圣殿祭司的ASP.NET.2.0.开发详解-使用C#
Tram sex maniac 2 (H) rar bag10
美女写真2
Unix video tutorial2
电脑知识及技巧大合集
delv published in(发表于) 2014/1/16 9:32:48 Edit(编辑)
实例分享:自己开发的自定义分页控件_[Asp.Net教程]

实例分享:自己开发的自定义分页控件_[Asp.Net教程]

实例分享:自己开发的自定义分页控件_[Asp.Net教程]

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


  (另外注意:在控件编译时,可以在AssemblyInfo.cs文件中设置控件的标签和名称空间,如:


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





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

[DefaultProperty("Text"),
ToolboxData("")]这句屏蔽掉



所有代码如下:


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(Pageindex0)
{
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 = "
";

Text +=""+First+"&nbsp;";

Text +=""+Pres+"&nbsp;";

Text +=""+Pre+"&nbsp;";

Text +="Pagestring";

Text +=""+Next+"&nbsp;";

Text +=""+Nexts+"&nbsp;";

Text +=""+Last+"&nbsp;";

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()+"&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其他函数
///
///判断是否为数字字符串
///

///需验证的字符串
///判断结果,符合条件为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
}
}
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Web.UI;
[assembly: TagPrefix("PublicControls","PublicControls")] // 自定义控件前缀

来源:网络







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.