shawl.qiu c# .net DataList, Repeater, DataGrid 分页类 v1.0
2007-02-09 18:57
621 查看
shawl.qiu c# .net DataList, Repeater, DataGrid 分页类 v1.0
说明:其实 DataGrid 本身就自带分页...不过鄙人很讨厌 Table, 虽然也可以设置为Flow, 但灵活性还是打折了, 应用虽然非常方便, 但是不灵活.
从自由点来看, 鄙人比较喜欢灵活性较高的 DataList, 可发觉没自带分页功能, 这大概就是什么鱼与熊掌, 不可兼得的原因吧...
因此这个类就诞生了...
Pagination 类 使用重载, 使得使用最少的代码可让分页功能应用于:
DataList, Repeater, DataGrid.
目录:
1. x.aspx
2. cs/Pagination.cs
下载原格式:
http://files.myopera.com/btbtd/csharp/class/sq_csharp_pagination_class_v1.0.7z
shawl.qiu
2007-02-09
http://blog.csdn.net/btbtd
内容:
1. x.aspx
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Assembly src="cs/Pagination.cs" %>
<%@ import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>shawl.qiu template</title>
<style type="text/css">
/*<![CDATA[*/
.sqPagedCurLink{
color: #FFCCFF;
font-weight:bold;
}
.sqPagedNonLink{
color:#ccc;
}
.sqPagedJumpBox{
border:0px;
border-bottom:1px dashed black;
width:48px;
}
/*]]>*/
</style>
<script runat="server">
void Page_Load(Object s, EventArgs e)
{
DataTable dt =new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("id", typeof(Int32)));
dt.Columns.Add(new DataColumn("item", typeof(String)));
for(Int32 i=0; i<222; i++)
{
dr=dt.NewRow();
dr[0]=i;
dr[1]="item "+i.ToString();
dt.Rows.Add(dr);
}
Pagination Paged=new Pagination();
Paged.Debug=false; // 是否为调试模式
Paged.DebugLabel=glrDebugLabel; // 显示调试信息的 Label
Paged.NavigatorLabel=glrPagedLabel; // 显示主导航条的 Label
Paged.DetailsLabel=glrPagedDetailsLabel;// 显示分页明细的 Label
Paged.PageSize=10; // 每页大小
Paged.Go(dt, glrFileList); // 执行分页
Paged=null;
} // end Page_Load
</script>
</head>
<body>
<form runat="server">
<div class="algc">
<asp:Label id="glrDebugLabel" runat="server" />
</div>
<div class="algc">
<asp:Label id="glrPagedLabel" runat="server" />
<div class="algc">
<asp:Label id="glrPagedDetailsLabel" runat="server" />
</div>
</div>
<asp:DataList id="glrFileList"
BorderColor="black"
CellPadding="5"
CellSpacing="5"
RepeatDirection="Horizontal"
RepeatLayout="Flow"
RepeatColumns="5"
ShowBorder="True"
runat="server">
<HeaderTemplate>
<div>
</HeaderTemplate>
<HeaderStyle BackColor="#aaaadd">
</HeaderStyle>
<AlternatingItemStyle BackColor="Gainsboro">
</AlternatingItemStyle>
<ItemTemplate>
<a href="?id=<%# DataBinder.Eval(Container.DataItem, "id") %>">
<%# DataBinder.Eval(Container.DataItem, "item") %>
</a>
</ItemTemplate>
<%--
<SeparatorTemplate>
</SeparatorTemplate>
--%>
<FooterTemplate>
</div>
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>
2. cs/Pagination.cs
using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
/*-----------------------------------------------------------------------------------*/
* shawl.qiu c# .net Pagination class v1.0
/*-----------------------------------------------------------------------------------*/
//---------------------------------------------------------------------begin class Pagination
public class Pagination
{
//-----------------------------------begin event
public Pagination()
{
}
~Pagination()
{
}
//-----------------------------------end event
//-----------------------------------begin public constant
//-----------------------begin about
public const String auSubject="shawl.qiu c# .net Pagination class";
public const String auVersion="v1.0";
public const String au="shawl.qiu";
public const String auEmail="shawl.qiu@gmail.com";
public const String auBlog="http://blog.csdn.net/btbtd";
public const String auCreateDate="2007-2-9";
//-----------------------end about
//-----------------------------------end public constant
//-----------------------------------begin public variable
public Int32 PageSize=10;
public Int32 CurrentPage=0;
public Int32 ListNumSize=10;
public String QueryId="page";
public Boolean Debug=false;
public Label DebugLabel;
public Label NavigatorLabel;
public Label DetailsLabel;
public String WordFirst="首页";
public String WordPreviousTen="上十页";
public String WordPrevious="上一页";
public String WordNext="下一页";
public String WordNextTen="下十页";
public String WordLast="末页";
//-----------------------------------end public variable
//-----------------------------------begin private variable
private Int32 PageCount=0;
private Int32 DataSourceCount=0;
private String QueryUrl="";
//-----------------------------------end private variable
//-----------------------------------begin public method
public void Go(DataTable dtDataTable, DataList dsDataList)
{
if(dtDataTable==null||dsDataList==null)goto End;
dsDataList.DataSource=objPagedDataSource(dtDataTable);
dsDataList.DataBind();
End:;
}
public void Go(DataTable dtDataTable, Repeater rptRepeater)
{
if(dtDataTable==null||rptRepeater==null)goto End;
rptRepeater.DataSource=objPagedDataSource(dtDataTable);
rptRepeater.DataBind();
End:;
}
public void Go(DataTable dtDataTable, DataGrid dgDataGrid)
{
if(dtDataTable==null||dgDataGrid==null)goto End;
dgDataGrid.DataSource=objPagedDataSource(dtDataTable);
dgDataGrid.DataBind();
End:;
}
//-----------------------------------end public method
//-----------------------------------begin private method
private PagedDataSource objPagedDataSource(DataTable dtDataTable)
{
PagedDataSource dtPaged=new PagedDataSource();
dtPaged.DataSource = dtDataTable.DefaultView;
dtPaged.AllowPaging = true;
dtPaged.PageSize = PageSize;
DataSourceCount=dtPaged.DataSourceCount;
Int32 PageCount=dtPaged.PageCount;
if(System.Web.HttpContext.Current.Request.QueryString[QueryId]!=null)
{
String sTemp=System.Web.HttpContext.Current.Request.QueryString[QueryId];
CurrentPage=Int32.Parse(sTemp);
}
if(CurrentPage+1>PageCount)
{
CurrentPage=PageCount;
}
if(CurrentPage<1)
{
CurrentPage=1;
}
AutoConvertPagedUrl(QueryId, out QueryUrl);
if(Debug&&DebugLabel!=null)
{
DebugLabel.Text+="<li/>总记录数: "+DataSourceCount;
DebugLabel.Text+="<li/>总页数: "+PageCount;
DebugLabel.Text+="<li/>当前页: "+CurrentPage;
DebugLabel.Text+="<li/>每页大小: "+PageSize;
DebugLabel.Text+="<li/>QueryUrl "+QueryUrl;
DebugLabel.Text+="<hr/>";
}
if(NavigatorLabel!=null)
{
if(CurrentPage!=1)
{
NavigatorLabel.Text="<a href='"+QueryUrl+"1'>"+WordFirst+"</a> ";
}
else
{
NavigatorLabel.Text+="<span class='sqPagedNonLink'>"+WordFirst+"</span> ";
}
if(Debug)
{
DebugLabel.Text+="<li/>"+WordPreviousTen+": "+(CurrentPage-(CurrentPage%10)-10);
DebugLabel.Text+="<hr/>";
}
if(CurrentPage-(CurrentPage%10)-10>=0)
{
NavigatorLabel.Text+="<a href='"+QueryUrl+
(CurrentPage-(CurrentPage%10)-10+1)+"'>"+WordPreviousTen+"</a> ";
}
else
{
NavigatorLabel.Text+="<span class='sqPagedNonLink'>"+WordPreviousTen+"</span> ";
}
if(CurrentPage>1)
{
NavigatorLabel.Text+="<a href='"+QueryUrl+(CurrentPage-1)+"'>"+WordPrevious+"</a> ";
}
else
{
NavigatorLabel.Text+="<span class='sqPagedNonLink'>"+WordPrevious+"</span> ";
}
Int32 LoopNum=CurrentPage-(CurrentPage%10)+1;
if(Debug)
{
DebugLabel.Text+="<li/>数字链接循环起始: "+LoopNum;
DebugLabel.Text+="<hr/>";
}
for(Int32 i=1; i<=ListNumSize; i++)
{
if(LoopNum>PageCount)break;
if(LoopNum==CurrentPage)
{
NavigatorLabel.Text+="<span class='sqPagedCurLink'>"+(LoopNum)+"</span> ";
}
else
{
NavigatorLabel.Text+="<a href='"+QueryUrl+(LoopNum)+"'>"+(LoopNum)+"</a> ";
}
LoopNum++;
}
if(CurrentPage<PageCount)
{
NavigatorLabel.Text+="<a href='"+QueryUrl+(CurrentPage+1)+"'>"+WordNext+"</a> ";
}
else
{
NavigatorLabel.Text+="<span class='sqPagedNonLink'>"+WordNext+"</span> ";
}
if(Debug)
{
DebugLabel.Text+="<li/>下十页: "+(CurrentPage-(CurrentPage%10)+10);
DebugLabel.Text+="<hr/>";
}
if(CurrentPage-(CurrentPage%10)+10<=PageCount)
{
NavigatorLabel.Text+="<a href='"+QueryUrl+
(CurrentPage-(CurrentPage%10)+11)+"'>"+WordNextTen+"</a> ";
}
else
{
NavigatorLabel.Text+="<span class='sqPagedNonLink'>"+WordNextTen+"</span> ";
}
if(CurrentPage!=PageCount)
{
NavigatorLabel.Text+="<a href='"+QueryUrl+(PageCount)+"'>"+WordLast+"</a> ";
}
else
{
NavigatorLabel.Text+="<span class='sqPagedNonLink'>"+WordLast+"</span> ";
}
PagedJumpBox(NavigatorLabel, QueryUrl);
}
if(DetailsLabel!=null)
{
DetailsLabel.Text=
PageSize+"篇/页 "+
(CurrentPage)+"/"+(PageCount)+
"页 共"+DataSourceCount+"篇";
}
dtPaged.CurrentPageIndex =(CurrentPage-1);
return dtPaged;
} // end private PagedDataSource objPagedDataSource
private void PagedJumpBox(Label NavigatorLabel, String QueryUrl)
{
NavigatorLabel.Text+="转到:/n";
NavigatorLabel.Text+=
"<input type='text' onsubmit='return false;' size='6' "+
"class='sqPagedJumpBox' onkeypress='fG2Url(this.value, this.form,event)' />/n";
NavigatorLabel.Text+="<script type='text/javascript'>/n";
NavigatorLabel.Text+="//<![CDATA[/n";
NavigatorLabel.Text+=" function fG2Url(sUrl, oForm, oEvt){/n";
NavigatorLabel.Text+=" oForm.onsubmit=function()/n";
NavigatorLabel.Text+=" {/n";
NavigatorLabel.Text+=" return false;/n";
NavigatorLabel.Text+=" }/n";
NavigatorLabel.Text+=" if(!oEvt)var oEvt=window.event;/n";
NavigatorLabel.Text+=" var kc=oEvt.which||oEvt.keyCode;/n";
NavigatorLabel.Text+=" if(kc==13){/n";
NavigatorLabel.Text+=@" sUrl=sUrl.replace(/^/s+|/s+$/g, '');";
NavigatorLabel.Text+=@" sUrl=parseInt(sUrl)||1;";
NavigatorLabel.Text+=@" window.location.href='"+QueryUrl+"'+sUrl}";
NavigatorLabel.Text+=" }/n";
NavigatorLabel.Text+="//]]>/n";
NavigatorLabel.Text+="</script>/n";
} // end private void PagedJumpBox
private void AutoConvertPagedUrl(String QueryId, out String QueryUrl)
{
QueryUrl=System.Web.HttpContext.Current.Request.Url+"";
if(QueryUrl.IndexOf(@"?")==-1)
{
QueryUrl+=@"?";
}
QueryUrl=Regex.Replace(QueryUrl,@"/b"+@QueryId+@"/=[^&]+","", RegexOptions.IgnoreCase);
QueryUrl+="&"+QueryId+"=";
QueryUrl=Regex.Replace(QueryUrl,@"/?/&",@"?", RegexOptions.IgnoreCase);
QueryUrl=Regex.Replace(QueryUrl,@"/&+",@"&", RegexOptions.IgnoreCase);
} // end private void AutoConvertPagedUrl
//-----------------------------------end private method
}
//---------------------------------------------------------------------end class Pagination
相关文章推荐
- shawl.qiu c# .net 发邮件用户控件 v1.0
- 决定何时使用 DataGrid、DataList 或 Repeater(ASP.NET 技术文章)
- ASP.NET REPEATER, LISTVIEW, DATALIST, DATAGRID, GRIDVIEW - WHICH ONE TO CHOOSE?
- c# .net 生成图片验证码 By shawl.qiu
- shawl.qiu c# .net 自动生成 OleDb 数据操作语句小工具 Producing OleDbSql 1.1
- Control Study -> Asp.net几个较复杂的显示数据控件(Repeater/DataList/DataGrid)用法 (示例代码下载)
- Control Study -> Asp.net几个较复杂数据控件(Repeater/DataList/DataGrid)用法 (示例代码下载)
- asp.net总结之Repeater,DataList和DataGrid
- c# .net 编写 HTML 编辑器的核心技巧 by shawl.qiu
- 决定何时使用 DataGrid、DataList 或 Repeater(ASP.NET 技术文章)
- c# .net 上传多个文件 By shawl.qiu
- ASP.NET中Repeater控件和DataList控件通用的分页类
- shawl.qiu c# .net 服务端表单验证类 FormValidator 1.1
- ASP.Net提供了三个数据控件,;这三个控件属于数据Web控件,分别是DataGrid,DataList和Repeater。
- Control Study -> Asp.net几个较复杂的显示数据控件(Repeater/DataList/DataGrid)用法 (示例代码下载)
- asp.net中GridView、DataGrid、DataList、Repeater、ListView、DetailsView、FormView 的区别
- shawl.qiu c# .net upload class v1.1
- asp.net DataList 复选框 也玩 全选反选全不选 by shawl.qiu
- c# .net 生成缩略图(按比例) By shawl.qiu
- Control Study -> Asp.net几个较复杂的显示数据控件(Repeater/DataList/DataGrid)用法 (示例代码下载)