asp.net无刷新分页实例代码
数据类代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Collections;
usingSystem.Reflection;
namespaceDAL
{
public classUserManageClass
{
///<summary>
///取得总页数
///</summary>
///<returns>总页数</returns>
publicintGetPageCount()
{
intcounts;
stringSqlStr="selectcount(0)from[User]";
counts=newSQLHelper().Content(SqlStr,CommandType.Text);
returncounts;
}
///<summary>
///取出每一页的内容
///</summary>
///<paramname="SatrPage">开始页数</param>
///<paramname="EndPage">结束页数</param>
///<returns>每一页的内容</returns>
publicDataTableGetPageDate(stringSatrPage,stringEndPage)
{
DataTabledt;
stringSqlStr=@"select*from
(select*,ROW_NUMBER()over(orderbyid)asno_from[User])aa
whereaa.no_between""+SatrPage+""and""+EndPage+""";
dt=newSQLHelper().ExecuteQuery(SqlStr,CommandType.Text);
returndt;
}
///<summary>
///将一个DataTable转换成列表
///</summary>
///<typeparamname="T">实体对象的类型</typeparam>
///<paramname="dt">要转换的DataTable</param>
///<returns></returns>
public List<T>DataTableToEntityList<T>(DataTabledt)
{
List<T>entiyList=newList<T>();
TypeentityType=typeof(T);
PropertyInfo[]entityProperties=entityType.GetProperties();
foreach(DataRowrowindt.Rows)
{
Tentity=Activator.CreateInstance<T>();
foreach(PropertyInfopropInfoinentityProperties)
{
if(dt.Columns.Contains(propInfo.Name))
{
if(!row.IsNull(propInfo.Name))
{
propInfo.SetValue(entity,row[propInfo.Name],null);
}
}
}
entiyList.Add(entity);
}
returnentiyList;
}
}
}
PageService.ashx.cs一般处理程序代码:
usingSystem;
usingSystem.Collections;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Services;
usingSystem.Web.Services.Protocols;
usingSystem.Xml.Linq;
usingSystem.Data.SqlClient;
usingDAL;
usingSystem.Web.Extensions;
usingSystem.Web.Script.Serialization;
usingModel;
usingSystem.Web.UI.MobileControls;
usingSystem.Collections.Generic;
namespaceLandingSystem
{
///<summary>
///$codebehindclassname$的摘要说明
///</summary>
[WebService(Namespace="http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
publicclassPageService:IHttpHandler
{
publicvoidProcessRequest(HttpContextcontext)
{
context.Response.ContentType="text/plain";
stringaction=context.Request["action"];
if(action=="GetPageCount")
{
intcounts=newUserManageClass().GetPageCount();
intpage=counts/3;
if(counts%3!=0)
{
page++;
}
context.Response.Write(page);
}
elseif(action=="GetPageData")
{
intpageNo=Convert.ToInt32(context.Request["PageNo"]);
stringSatrPage=((pageNo-1)*3+1).ToString();
stringEndPage=(pageNo*3).ToString();
DataTabledt=newUserManageClass().GetPageDate(SatrPage,EndPage);
IList<RegisterModel>data=ModelConvertHelper<RegisterModel>.ConvertToModel(dt);
//IList<RegisterModel>data=newUserManageClass().DataTableToEntityList<RegisterModel>(dt);
varp1=data.Select(c=>new{c.Name,c.Phone});
#region废物代码
//varp1=data.Select(c=>new{c.Name,c.Phone});
//varp1=data.Select(dr=>new{dr["Name"].ToString(),dr["Phone"].ToString()});
//varT_model=newList<RegisterModel>();
//varp3=T_model.Select(c=>new{c.Name,c.Phone});
//varp2=data.Select(c=>new{})
#endregion
JavaScriptSerializerjss=newJavaScriptSerializer();
context.Response.Write(jss.Serialize(p1));
}
}
publicboolIsReusable
{
get
{
returnfalse;
}
}
}
}
aspx页面代码:
<scriptsrc="JS/jquery-latest.js"type="text/javascript"></script> ModelConvertHelper.cs(将datatable转换为list通用类)代码: namespaceDAL
<headrunat="server">
<title>无标题页</title>
<scripttype="text/javascript">
$(function(){
//-----------------------------------------------------------
functiongetPageData(pageNo){//取得某页数据的方法
$.post("PageService.ashx",{"action":"GetPageData","PageNo":pageNo},function(data,status){
if(status=="success"){
$("#Comment").empty();
varcomments=$.parseJSON(data);//反序列化json数据。
for(vari=0;i<comments.length;i++){
varrow=comments[i];
varli=$("<li>"+row.Name+":"+row.Phone+"</li>");
$("#Comment").append(li);//每取出一条数据就创建一个li并append到Comment/ul内。
}
}
});
}
//-------------------------------------------------------------------
getPageData(1);//首次进入页面,看到的是第一页的数据
//----------------------------------------------------------------/
//取得所有的页数并且初始化分页按钮
$.post("PageService.ashx",{"action":"GetPageCount"},function(data,status){
if(status=="success"){
vartr1=$("<tr></tr>");
varpageNo=parseInt(data);
for(vari=1;i<=pageNo;i++){
vartd=$("<td><ahref="">"+i+"</a></td>");
tr1.append(td);
}
$("#pageNo").append(tr1);
$("#pageNoa").click(function(e){//页码创建后,就为每一个页码监听一个click事件。
e.preventDefault();//取消a的默认跳转行为
getPageData($(this).html());//点击后就去执行取页数据的操作。
});
}
});
//----------------------------------------------------------------------------
});
</script>
</head>
<body>
<table>
<tr>
<td>
<ulid="Comment"></ul>
</td>
</tr>
</table>
<br/>
页数:
<tableid="pageNo"></table>
</body>
</html>
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Collections;
usingSystem.Data;
usingSystem.Reflection;
{
publicclassModelConvertHelper<T>whereT:new()
{
publicstaticIList<T>ConvertToModel(DataTabledt)
{
IList<T>ts=newList<T>();
Typetype=typeof(T);
stringtempName="";
foreach(DataRowdrindt.Rows)
{
Tt=newT();
//获得此模型的公共属性
PropertyInfo[]propertys=t.GetType().GetProperties();
foreach(PropertyInfopiinpropertys)
{
tempName=pi.Name;
//检查DataTable是否包含此列
if(dt.Columns.Contains(tempName))
{
//判断此属性是否有Setter
if(!pi.CanRead)continue;
objectvalue=dr[tempName];
if(value!=DBNull.Value)
if(pi.PropertyType==typeof(int))
{
pi.SetValue(t,Convert.ToInt32(value),null);
}
elseif(pi.PropertyType==typeof(string))
{
pi.SetValue(t,value.ToString(),null);
}
//pi.SetValue(t,value,null);
}
}
ts.Add(t);
}
returnts;
}
}
}相关文章