asp.net 無刷新分頁實例代碼
數(shù)據(jù)類代碼:
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>
/// 取得總頁數(shù)
/// </summary>
/// <returns>總頁數(shù)</returns>
public int GetPageCount()
{
int counts;
string SqlStr = "select count(0) from [User]";
counts = new SQLHelper().Content(SqlStr, CommandType.Text);
return counts;
}
/// <summary>
/// 取出每一頁的內(nèi)容
/// </summary>
/// <param name="SatrPage">開始頁數(shù)</param>
/// <param name="EndPage">結(jié)束頁數(shù)</param>
/// <returns>每一頁的內(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>
/// 將一個DataTable轉(zhuǎn)換成列表
/// </summary>
/// <typeparam name="T">實體對象的類型</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$ 的摘要說明
/// </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頁面代碼:
<head runat="server">
<title>無標題頁</title>
<script src="JS/jquery-latest.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
//-----------------------------------------------------------
function getPageData(pageNo){ //取得某頁數(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)建一個li并append到Comment/ul內(nèi)。
}
}
});
}
//-------------------------------------------------------------------
getPageData(1); //首次進入頁面,看到的是第一頁的數(shù)據(jù)
//----------------------------------------------------------------/
//取得所有的頁數(shù)并且初始化分頁按鈕
$.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){ //頁碼創(chuàng)建后,就為每一個頁碼監(jiān)聽一個click事件。
e.preventDefault(); //取消a的默認跳轉(zhuǎn)行為
getPageData($(this).html()); //點擊后就去執(zhí)行取頁數(shù)據(jù)的操作。
});
}
});
//----------------------------------------------------------------------------
});
</script>
</head>
<body>
<table>
<tr>
<td>
<ul id="Comment"></ul>
</td>
</tr>
</table>
<br />
頁數(shù):
<table id="pageNo"></table>
</body>
</html>
ModelConvertHelper.cs(將datatable轉(zhuǎn)換為list通用類)代碼:
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通用無刷新Ajax分頁控件,支持多樣式多數(shù)據(jù)綁定
- 用AJAX實現(xiàn)的無刷新的分頁實現(xiàn)代碼(asp.net)
- asp.net jquery無刷新分頁插件(jquery.pagination.js)
- asp.net中利用Jquery+Ajax+Json實現(xiàn)無刷新分頁的實例代碼
- asp.net使用AJAX實現(xiàn)無刷新分頁
- asp.net gridview分頁:第一頁 下一頁 1 2 3 4 上一頁 最末頁
- asp.net實現(xiàn)簡單分頁實例
- asp.net中如何調(diào)用sql存儲過程實現(xiàn)分頁
- ASP.NET無刷新分頁簡單實現(xiàn)
相關文章
jQuery 插件autocomplete自動完成應用(自動補全)(asp.net后臺)
項目中有時會用到自動補全查詢,就像Google搜索框、淘寶商品搜索功能,輸入漢字或字母,則以該漢字或字母開頭的相關條目會顯示出來供用戶選擇, autocomplete插件就是完成這樣的功能2011-10-10ASP.NET下將Excel表格中的數(shù)據(jù)規(guī)則的導入數(shù)據(jù)庫思路分析及實現(xiàn)
今天接到新的需求,要求將Excel表格中的數(shù)據(jù)顯示在頁面上個人想法:首先是規(guī)則的Excel數(shù)據(jù)導入,再有就是不規(guī)則的Excel數(shù)據(jù)導入,還有就是根據(jù)數(shù)據(jù)生成Excel2013-01-01AspNetPager分頁控件UrlRewritePattern參數(shù)設置的重寫代碼
AspNetPager分頁控件UrlRewritePattern參數(shù)設置的重寫代碼,需要的朋友可以參考一下2013-02-02MVC項目結(jié)構(gòu)搭建及單個類的實現(xiàn)學習筆記1
這篇文章主要介紹了MVC項目結(jié)構(gòu)搭建及單個類在各個層次中的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09ASP.Net使用System.Security.Principal模擬用戶
這篇文章介紹了ASP.Net使用System.Security.Principal模擬用戶的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05