效果图:http://www.ukei.cn/ob1.html
代码这里下载/Files/genson/AjaxPager.rar
l准备工作:
- jQuery,到http://www.jquery.com下载。
- 分页的存储过程 准备工作:http://www.cnblogs.com/genson/archive/2006/01/17/318882.html这里可以找到。
- 分页用户控件,首先我们先写一个基类继承 UserControl,主要是分页的属性和一个静态方法,代码如下
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Collections;
using
System.Text;
using
System.Collections.Specialized;


/**/
///<summary>
///SummarydescriptionforUserControlBase
///</summary>
public
abstract
class
DataUserControlBase:UserControl
{
publicintrecordCount=0;

publicabstractvoidBindData();
protectedoverridevoidOnLoad(EventArgse)

{
stringp=Request.QueryString["p"];
if(!string.IsNullOrEmpty(p))

{
PageIndex=Convert.ToInt32(p);
}
else

{
PageIndex=1;
}
BindData();
if(!Page.ClientScript.IsClientScriptIncludeRegistered(typeof(Page),"PagerCheck"))

{
Page.ClientScript.RegisterClientScriptInclude(typeof(Page),"PagerCheck",ResolveClientUrl("~/js/PagerCheck.js"));
}
base.OnLoad(e);
}


privateobject[]urlVariantParameters;
/**////<summary>
///页面传递多个参数的时候使用,这个要跟UrlParamers替换的数组一致,当参数只有一个的时候,可以省略
///</summary>
publicobject[]UrlVariantParameters

{
get
{returnurlVariantParameters;}
set
{urlVariantParameters=value;}
}
privatestringstrWhere=string.Empty;
publicstringStrWhere

{
get

{
returnstrWhere;
}
set

{
strWhere=value;
}
}
privateboolorderType=true;
publicboolOrderType

{
get
{returnorderType;}
set
{orderType=value;}
}
privatestringfldName="DateCreated";
publicstringFldName

{
get
{returnfldName;}
set
{fldName=value;}
}
privateint_recordCount;
publicvirtualintRecordCount

{
get

{
return_recordCount;
}
set

{
_recordCount=value;
}
}

privateintpageIndex=1;
publicvirtualintPageIndex

{
get

{
returnpageIndex;
}
set

{
pageIndex=value;
}
}


publicvirtualintPageCount

{
get

{
returnRecordCount%PageSize==0?RecordCount/PageSize:RecordCount/PageSize+1;
}
}

privatestringurlParamers;
/**////<summary>
///指通过页面传递参数的地址:如UrlParamers="~/ob{0}.html";
///</summary>
publicvirtualstringUrlParamers

{
get

{
returnurlParamers;
}
set

{
urlParamers=value;
}
}

privateintpageSize=10;
publicvirtualintPageSize

{
get

{
returnpageSize;
}
set

{
pageSize=value;
}
}
privateboolajaxEnabled=true;
/**////<summary>
///是否启用Ajax效果分页,默认启用
///</summary>
publicvirtualboolAjaxEnabled

{
get

{
returnajaxEnabled;
}
set

{
ajaxEnabled=value;
}
}

publicstaticvoidSetProperty(DataUserControlBasepager,DataUserControlBaseb)

{
b.RecordCount=b.recordCount;
pager.RecordCount=b.RecordCount;
pager.PageSize=b.PageSize;
pager.PageIndex=b.PageIndex;
pager.UrlParamers=b.UrlParamers;
pager.AjaxEnabled=b.AjaxEnabled;
pager.UrlVariantParameters=b.UrlVariantParameters;
}



}
- 然后我们再新增一个ascx文件,叫Pager.ascx吧!继承上面定义的DataUserControlBase,代码如下。
<%
@ControlLanguage="C#"AutoEventWireup="true"CodeFile="Pager.ascx.cs"Inherits="UC_Pager"
%>
<!--
这里开始输出页码条
-->
<
DIV
class
="pagebar"
>
<
asp:PlaceHolder
runat
=server
ID
=ph
/>
</
DIV
>
<!--
这里结束页码的输出
-->



<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingObj.DAL;
publicpartialclassUC_Pager:DataUserControlBase

{
protectedvoidPage_Load(objectsender,EventArgse)

{
GeneratePagerLink(ph);
}



publicvirtualvoidGeneratePagerLink(Controlcontainer)

{
intmax=PagerHelper.MaxPageIndex(PageIndex);
intmin=PagerHelper.MinPageIndex(PageIndex);
//if(string.IsNullOrEmpty(VirtualCurrentPageUrl))
//{
//thrownewException("VirtualCurrentPageUrlisnull");
//}
if(PageCount<=max)max=PageCount;
if(PageCount==1)return;

if(min!=1)//Prev

{
HyperLinkhyprev=newHyperLink();
hyprev.Text="<<";
if(UrlVariantParameters==null)

{
hyprev.NavigateUrl=string.Format(UrlParamers,min-1);
}
else

{
UrlVariantParameters[0]=min-1;
hyprev.NavigateUrl=string.Format(UrlParamers,UrlVariantParameters);
}
ConvertAnchor(hyprev,min-1);
container.Controls.Add(hyprev);
}
for(inti=min;i<=max;i++)

{
HyperLinkhy=newHyperLink();
if(UrlVariantParameters==null)

{
hy.NavigateUrl=string.Format(UrlParamers,i);
}
else

{
UrlVariantParameters[0]=i;
hy.NavigateUrl=string.Format(UrlParamers,UrlVariantParameters);
}
hy.Text=i.ToString();
if(PageIndex==i)
hy.NavigateUrl=string.Empty;
else
ConvertAnchor(hy,i);
container.Controls.Add(hy);

}
if(PageCount>max)//Next

{
HyperLinkhynext=newHyperLink();
hynext.Text=">>";
if(UrlVariantParameters==null)

{
hynext.NavigateUrl=string.Format(UrlParamers,max+1);
}
else

{
UrlVariantParameters[0]=max+1;
hynext.NavigateUrl=string.Format(UrlParamers,UrlVariantParameters);
}
ConvertAnchor(hynext,max+1);
container.Controls.Add(hynext);
}
}


privatevoidConvertAnchor(HyperLinkhy,intp)

{
if(AjaxEnabled)

{
hy.Attributes.Add("onclick",hy.NavigateUrl);
hy.NavigateUrl="#"+hy.NavigateUrl;
}
}

publicvirtualControlContainer

{
get

{
returnph;
}
}


publicoverridevoidBindData()

{
}
protectedoverridevoidOnPreRender(EventArgse)

{
if(RecordCount==0)

{
//Container.Controls.Add(newLiteralControl("<pstyle=text-align:center;>没有数据</p>"));
}
base.OnPreRender(e);
}
}
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.Data.SqlClient;
usingMicrosoft.ApplicationBlocks.Data;
namespaceObj.DAL

{
/**////<summary>
///SummarydescriptionforPageHelper
///</summary>

/**//**/
/**////<summary>
///分页类PagerHelper的摘要说明。
///</summary>
publicclassPagerHelper

{
privatestringconnectionString;

/**////<summary>
///得到记录集的构造函数
///</summary>
///<paramname="tblname"></param>
///<paramname="sortname"></param>
///<paramname="docount"></param>
///<paramname="connectionString"></param>
publicPagerHelper(stringtblname,stringsortname,booldocount,stringconnectionString)

{
this.tblName=tblname;
this.fldName=sortname;
this.connectionString=connectionString;
this.docount=docount;
}

/**////<summary>
///所有参数都有
///</summary>
///<paramname="tblname"></param>
///<paramname="docount"></param>
///<paramname="strGetFields"></param>
///<paramname="fldName"></param>
///<paramname="pagesize"></param>
///<paramname="pageindex"></param>
///<paramname="ordertype"></param>
///<paramname="strwhere"></param>
///<paramname="connectionString"></param>
publicPagerHelper(stringtblname,booldocount,
stringstrGetFields,stringfldName,intpagesize,
intpageindex,boolordertype,stringstrwhere,stringconnectionString
)

{
this.tblName=tblname;
this.docount=docount;
this.strGetFields=strGetFields;
this.fldName=fldName;
this.pagesize=pagesize;
this.pageindex=pageindex;
this.ordertype=ordertype;
this.strwhere=strwhere;
this.connectionString=connectionString;

}


/**//**/
/**////<summary>
///得到记录集的构造函数
///</summary>
///<paramname="tblname"></param>
///<paramname="strwhere"></param>
///<paramname="connectionString"></param>
publicPagerHelper(stringtblname,stringstrwhere,stringconnectionString)

{
this.tblName=tblname;
this.strwhere=strwhere;
this.docount=true;
this.connectionString=connectionString;
}
privatestringtblName;
publicstringTblName

{
get
{returntblName;}
set
{tblName=value;}
}
privatestringstrGetFields="*";
publicstringStrGetFields

{
get
{returnstrGetFields;}
set
{strGetFields=value;}
}
privatestringfldName=string.Empty;
publicstringFldName

{
get
{returnfldName;}
set
{fldName=value;}
}


privateintpagesize=10;
publicintPageSize

{
get
{returnpagesize;}
set
{pagesize=value;}
}
privateintpageindex=1;
publicintPageIndex

{
get
{returnpageindex;}
set
{pageindex=value;}
}

privatebooldocount=false;
publicboolDoCount

{
get
{returndocount;}
set
{docount=value;}
}
privateboolordertype=false;
publicboolOrderType

{
get
{returnordertype;}
set
{ordertype=value;}
}
privatestringstrwhere=string.Empty;
publicstringStrWhere

{
get
{returnstrwhere;}
set
{strwhere=value;}
}





publicIDataReaderGetDataReader()

{
if(this.docount)

{
thrownewArgumentException("要返回记录集,DoCount属性一定为false");
}


//System.Web.HttpContext.Current.Response.Write(pageindex);
returnSqlHelper.ExecuteReader(connectionString,CommandType.StoredProcedure,"Pagination",
newSqlParameter("@tblName",this.tblName),
newSqlParameter("@strGetFields",this.strGetFields),
newSqlParameter("@fldName",this.fldName),
newSqlParameter("@PageSize",this.pagesize),
newSqlParameter("@PageIndex",this.pageindex),
newSqlParameter("@doCount",this.docount),
newSqlParameter("@OrderType",this.ordertype),
newSqlParameter("@strWhere",this.strwhere)
);
}
publicDataSetGetDataSet()

{
if(this.docount)

{
thrownewArgumentException("要返回记录集,DoCount属性一定为false");
}
returnSqlHelper.ExecuteDataset(connectionString,CommandType.StoredProcedure,"Pagination",
newSqlParameter("@tblName",this.tblName),
newSqlParameter("@strGetFields",this.strGetFields),
newSqlParameter("@fldName",this.fldName),
newSqlParameter("@PageSize",this.pagesize),
newSqlParameter("@PageIndex",this.pageindex),
newSqlParameter("@doCount",this.docount),
newSqlParameter("@OrderType",this.ordertype),
newSqlParameter("@strWhere",this.strwhere)
);
}

publicintGetCount()

{
if(!this.docount)

{
thrownewArgumentException("要返回总数统计,DoCount属性一定为true");
}


return(int)SqlHelper.ExecuteScalar(connectionString,CommandType.StoredProcedure,"Pagination",
newSqlParameter("@tblName",this.tblName),
newSqlParameter("@strGetFields",this.strGetFields),
newSqlParameter("@fldName",this.fldName),
newSqlParameter("@PageSize",this.pagesize),
newSqlParameter("@PageIndex",this.pageindex),
newSqlParameter("@doCount",this.docount),
newSqlParameter("@OrderType",this.ordertype),
newSqlParameter("@strWhere",this.strwhere)
);
}

publicstaticintMaxPageIndex(intpageindex)

{
if(pageindex<10)
return10;
stringnumstr=pageindex.ToString();
numstr=numstr.Substring(0,numstr.Length-1);

returnConvert.ToInt32(numstr+"0")+10;
}
publicstaticintMinPageIndex(intpageindex)

{
if(pageindex<10)
return1;
stringnumstr=pageindex.ToString();
numstr=numstr.Substring(0,numstr.Length-1);
returnConvert.ToInt32(numstr+"0");
}
}
}
330

被折叠的 条评论
为什么被折叠?



