ASP.NET分页组件学与用——教学篇(源代码)

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.ComponentModel;

using System.Text;

namespace NSLzhPages

{

public class LzhPages : System.Web.UI.WebControls.WebControl

{

private int _count; //每页显示的记录条数

private int _currentPage; //当前页

private int _allCount; //所有记录条数

private int _showPages; //显示页数

//以下脚本用于从文本框输入页码

private const string SCRIPTSTRING = "

 1<script language="javascript">\n"  \+ 
 2
 3" function go(ctrl,max)\n"  \+ 
 4
 5"  {\n"  \+ 
 6
 7"  if(ctrl.value >= 1 && ctrl.value <= max)\n"  \+ 
 8
 9"  {\n"  \+ 
10
11"  var url;\n"  \+ 
12
13"  var index;\n"  \+ 
14
15"  url = location.href;\n"  \+ 
16
17"  index = url.indexOf('?');\n"  \+ 
18
19"  if(index == -1)\n"  \+ 
20
21"  {\n"  \+ 
22
23"  }\n"  \+ 
24
25"  else\n"  \+ 
26
27"  {\n"  \+ 
28
29"  url = url.substring(0,index);\n"  \+ 
30
31"  }\n"  \+ 
32
33"  location.href = url + '?currentPage=' + ctrl.value;\n"  \+ 
34
35"  }\n"  \+ 
36
37"  else\n"  \+ 
38
39"  {\n"  \+ 
40
41"  alert('您输入的页码必须是符合页面要求的数字,最大值是:' + max);\n"  \+ 
42
43"  return false;\n"  \+ 
44
45"  }\n"  \+ 
46
47"  }\n"  \+ 
48
49"</script>

\n" ;

[DefaultValue(10),Category( "Customer" )]

public int Count

{

set

{

if ( value <= 0)

_count = 1;

else

_count = value ;

}

get

{

return _count;

}

}

[DefaultValue(1),Category( "Customer" )]

public int CurrentPage

{

set

{

if ( value < 0)

_currentPage = 1;

else

_currentPage = value ;

}

get

{

return _currentPage;

}

}

[DefaultValue(1),Category( "Customer" )]

public int AllCount

{

get

{

return _allCount;

}

set

{

if (_allCount < 0)

throw new Exception( "记录总条数不能为负数" );

else

_allCount = value ;

}

}

[DefaultValue(1),Category( "Customer" )]

public int Pages //总页数

{

get

{

if ( this ._allCount % this ._count > 0)

return (( int ) this ._allCount / this ._count) + 1;

else

return (( int ) this ._allCount / this ._count);

}

}

[DefaultValue(1),Category( "Customer" )]

public int ShowPages

{

set

{

if ( value > 0)

_showPages = value ;

else

_showPages = 9;

}

get

{

return _showPages;

}

}

protected override void Render(HtmlTextWriter writer)

{

base .Render (writer);

string leftInfo;

StringBuilder centerInfo = new StringBuilder();

//分页条分三部分,leftInfo是最左边的部分,用来显示当前页/总页数,每页显示的记录条数

leftInfo = "页:" + this .CurrentPage.ToString() + "/" + this .Pages.ToString() + "  " + "每页" + this .Count.ToString() + "条" + "  共" + this .AllCount.ToString() + "条" ;

//中间的部分是分页部分

int min; //要显示的页面数最小值

int max; //要显示的页面数最大值

if ( this .CurrentPage > this .Pages) //当前页必须小于最大页

{

this .CurrentPage = this .Pages;

Published At
Categories with Web编程
Tagged with
comments powered by Disqus