asp.net 無(wú)刷新分頁(yè)實(shí)例代碼
數(shù)據(jù)類(lèi)代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Reflection;
namespace DAL
{
public class UserManageClass
{
/// <summary>
/// 取得總頁(yè)數(shù)
/// </summary>
/// <returns>總頁(yè)數(shù)</returns>
public int GetPageCount()
{
int counts;
string SqlStr = "select count(0) from [User]";
counts = new SQLHelper().Content(SqlStr, CommandType.Text);
return counts;
}
/// <summary>
/// 取出每一頁(yè)的內(nèi)容
/// </summary>
/// <param name="SatrPage">開(kāi)始頁(yè)數(shù)</param>
/// <param name="EndPage">結(jié)束頁(yè)數(shù)</param>
/// <returns>每一頁(yè)的內(nèi)容</returns>
public DataTable GetPageDate(string SatrPage, string EndPage)
{
DataTable dt;
string SqlStr = @"select * from
(select *, ROW_NUMBER() over(order by id)as no_ from [User])aa
where aa.no_ between '"+SatrPage+"' and '"+EndPage+"'";
dt = new SQLHelper().ExecuteQuery(SqlStr, CommandType.Text);
return dt;
}
/// <summary>
/// 將一個(gè)DataTable轉(zhuǎn)換成列表
/// </summary>
/// <typeparam name="T">實(shí)體對(duì)象的類(lèi)型</typeparam>
/// <param name="dt">要轉(zhuǎn)換的DataTable</param>
/// <returns></returns>
public List<T> DataTableToEntityList<T>(DataTable dt)
{
List<T> entiyList = new List<T>();
Type entityType = typeof(T);
PropertyInfo[] entityProperties = entityType.GetProperties();
foreach (DataRow row in dt.Rows)
{
T entity = Activator.CreateInstance<T>();
foreach (PropertyInfo propInfo in entityProperties)
{
if (dt.Columns.Contains(propInfo.Name))
{
if (!row.IsNull(propInfo.Name))
{
propInfo.SetValue(entity, row[propInfo.Name], null);
}
}
}
entiyList.Add(entity);
}
return entiyList;
}
}
}
PageService.ashx.cs一般處理程序代碼:
using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;
using DAL;
using System.Web.Extensions;
using System.Web.Script.Serialization;
using Model;
using System.Web.UI.MobileControls;
using System.Collections.Generic;
namespace LandingSystem
{
/// <summary>
/// $codebehindclassname$ 的摘要說(shuō)明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class PageService : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string action = context.Request["action"];
if (action == "GetPageCount")
{
int counts = new UserManageClass().GetPageCount();
int page = counts / 3;
if (counts % 3 != 0)
{
page++;
}
context.Response.Write(page);
}
else if (action == "GetPageData")
{
int pageNo = Convert.ToInt32(context.Request["PageNo"]);
string SatrPage = ((pageNo - 1) * 3 + 1).ToString();
string EndPage = (pageNo * 3).ToString();
DataTable dt= new UserManageClass().GetPageDate(SatrPage, EndPage);
IList<RegisterModel> data = ModelConvertHelper<RegisterModel>.ConvertToModel(dt);
// IList<RegisterModel> data = new UserManageClass().DataTableToEntityList<RegisterModel>(dt);
var p1 = data.Select(c => new { c.Name,c.Phone});
#region 廢物代碼
// var p1 = data.Select( c => new { c.Name,c.Phone});
//var p1=data.Select(dr=>new {dr["Name"].ToString(),dr["Phone"].ToString()});
//var T_model = new List<RegisterModel>();
//var p3 = T_model.Select(c => new { c.Name, c.Phone });
//var p2=data.Select(c=>new {})
#endregion
JavaScriptSerializer jss = new JavaScriptSerializer();
context.Response.Write(jss.Serialize(p1));
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
aspx頁(yè)面代碼:
<head runat="server">
<title>無(wú)標(biāo)題頁(yè)</title>
<script src="JS/jquery-latest.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
//-----------------------------------------------------------
function getPageData(pageNo){ //取得某頁(yè)數(shù)據(jù)的方法
$.post("PageService.ashx",{"action":"GetPageData","PageNo":pageNo},function(data,status){
if(status=="success"){
$("#Comment").empty();
var comments=$.parseJSON(data); //反序列化json數(shù)據(jù)。
for(var i=0;i<comments.length;i++){
var row=comments[i];
var li= $("<li>"+row.Name+" : "+row.Phone+"</li>");
$("#Comment").append(li); //每取出一條數(shù)據(jù)就創(chuàng)建一個(gè)li并append到Comment/ul內(nèi)。
}
}
});
}
//-------------------------------------------------------------------
getPageData(1); //首次進(jìn)入頁(yè)面,看到的是第一頁(yè)的數(shù)據(jù)
//----------------------------------------------------------------/
//取得所有的頁(yè)數(shù)并且初始化分頁(yè)按鈕
$.post("PageService.ashx",{"action":"GetPageCount"},function(data,status){
if(status=="success"){
var tr1=$("<tr></tr>");
var pageNo=parseInt(data);
for(var i=1;i<=pageNo;i++){
var td=$("<td><a href=''>"+i+"</a></td>");
tr1.append(td);
}
$("#pageNo").append(tr1);
$("#pageNo a").click(function(e){ //頁(yè)碼創(chuàng)建后,就為每一個(gè)頁(yè)碼監(jiān)聽(tīng)一個(gè)click事件。
e.preventDefault(); //取消a的默認(rèn)跳轉(zhuǎn)行為
getPageData($(this).html()); //點(diǎn)擊后就去執(zhí)行取頁(yè)數(shù)據(jù)的操作。
});
}
});
//----------------------------------------------------------------------------
});
</script>
</head>
<body>
<table>
<tr>
<td>
<ul id="Comment"></ul>
</td>
</tr>
</table>
<br />
頁(yè)數(shù):
<table id="pageNo"></table>
</body>
</html>
ModelConvertHelper.cs(將datatable轉(zhuǎn)換為list通用類(lèi))代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Reflection;
namespace DAL
{
public class ModelConvertHelper<T> where T : new ()
{
public static IList<T> ConvertToModel(DataTable dt)
{
IList<T> ts = new List<T>();
Type type=typeof(T);
string tempName = "";
foreach (DataRow dr in dt.Rows)
{
T t = new T();
// 獲得此模型的公共屬性
PropertyInfo[] propertys = t.GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name;
// 檢查DataTable是否包含此列
if (dt.Columns.Contains(tempName))
{
// 判斷此屬性是否有Setter
if (!pi.CanRead) continue;
object value = dr[tempName];
if (value != DBNull.Value)
if (pi.PropertyType == typeof(int))
{
pi.SetValue(t, Convert.ToInt32(value), null);
}
else if (pi.PropertyType == typeof(string))
{
pi.SetValue(t, value.ToString(), null);
}
//pi.SetValue(t, value, null);
}
}
ts.Add(t);
}
return ts;
}
}
}
- AspNetAjaxPager,Asp.Net通用無(wú)刷新Ajax分頁(yè)控件,支持多樣式多數(shù)據(jù)綁定
- 用AJAX實(shí)現(xiàn)的無(wú)刷新的分頁(yè)實(shí)現(xiàn)代碼(asp.net)
- asp.net jquery無(wú)刷新分頁(yè)插件(jquery.pagination.js)
- asp.net中利用Jquery+Ajax+Json實(shí)現(xiàn)無(wú)刷新分頁(yè)的實(shí)例代碼
- asp.net使用AJAX實(shí)現(xiàn)無(wú)刷新分頁(yè)
- asp.net gridview分頁(yè):第一頁(yè) 下一頁(yè) 1 2 3 4 上一頁(yè) 最末頁(yè)
- asp.net實(shí)現(xiàn)簡(jiǎn)單分頁(yè)實(shí)例
- asp.net中如何調(diào)用sql存儲(chǔ)過(guò)程實(shí)現(xiàn)分頁(yè)
- ASP.NET無(wú)刷新分頁(yè)簡(jiǎn)單實(shí)現(xiàn)
相關(guān)文章
asp.net 時(shí)間類(lèi) 一周的周一和周末的日期
asp.net中時(shí)間類(lèi)——得到一周的周一和周末的日期2008-12-12
ASP.NET(C#) String, StringBuilder 與 StringWriter性能比較
ASP.NET(C#) String, StringBuilder 與 StringWriter性能比較...2007-08-08
ASP.NET?Core通過(guò)Microsoft.AspNetCore.App元包簡(jiǎn)化程序集引用
這篇文章介紹了ASP.NET?Core通過(guò)Microsoft.AspNetCore.App元包簡(jiǎn)化程序集引用的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
ASP.NET MVC從控制器傳遞數(shù)據(jù)到視圖的四種方式詳解
本篇文章主要介紹了ASP.NET MVC從控制器傳遞數(shù)據(jù)到視圖的四種方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01
.Net中實(shí)現(xiàn)無(wú)限分類(lèi)的2個(gè)例子
這篇文章主要介紹了.Net中實(shí)現(xiàn)無(wú)限分類(lèi)的2個(gè)例子,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-02-02
在.NET中使用Newtonsoft.Json轉(zhuǎn)換,讀取,寫(xiě)入的方法介紹
Newtonsoft.Json.JsonConvert類(lèi)是非微軟提供的一個(gè)JSON序列化和反序列的開(kāi)源免費(fèi)的類(lèi)庫(kù)2012-08-08
ASP.Net中利用CSS實(shí)現(xiàn)多界面的兩種方法
這篇文章主要介紹了ASP.Net中利用CSS實(shí)現(xiàn)多界面的兩種方法,包括動(dòng)態(tài)加載css樣式與動(dòng)態(tài)設(shè)置頁(yè)面同類(lèi)控件的方法來(lái)實(shí)現(xiàn)該功能,是非常具有實(shí)用價(jià)值的技巧,需要的朋友可以參考下2014-12-12
.net core 靜態(tài)類(lèi)獲取appsettings的方法
這篇文章主要介紹了.net core 靜態(tài)類(lèi)獲取appsettings的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

