asp.net 無重新整理分頁執行個體代碼

來源:互聯網
上載者:User

資料類代碼:

複製代碼 代碼如下: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>
/// 取得總頁數
/// </summary>
/// <returns>總頁數</returns>
public int GetPageCount()
{
int counts;
string SqlStr = "select count(0) from [User]";
counts = new SQLHelper().Content(SqlStr, CommandType.Text);
return counts;
}
/// <summary>
/// 取出每一頁的內容
/// </summary>
/// <param name="SatrPage">開始頁數</param>
/// <param name="EndPage">結束頁數</param>
/// <returns>每一頁的內容</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轉換成列表
/// </summary>
/// <typeparam name="T">實體物件的類型</typeparam>
/// <param name="dt">要轉換的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){ //取得某頁資料的方法
$.post("PageService.ashx",{"action":"GetPageData","PageNo":pageNo},function(data,status){
if(status=="success"){
$("#Comment").empty();
var comments=$.parseJSON(data); //還原序列化json資料。
for(var i=0;i<comments.length;i++){
var row=comments[i];
var li= $("<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"){
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){ //頁碼建立後,就為每一個頁碼監聽一個click事件。
e.preventDefault(); //取消a的預設跳轉行為
getPageData($(this).html()); //點擊後就去執行取頁資料的操作。
});
}
});
//----------------------------------------------------------------------------
});
</script>
</head>
<body>
<table>
<tr>
<td>
<ul id="Comment"></ul>
</td>
</tr>
</table>
<br />
頁數:
<table id="pageNo"></table>
</body>
</html>

ModelConvertHelper.cs(將datatable轉換為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;
}

}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.