在asp.net编程中,分布是个永恒的话题,也是个不断有创新的话题,因为对于大数据量的分页,对性能的要求还是比较多的。
为了提高用户体检,可以尝试下无刷新的方案。
1、数据访问类
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;
}
}
}
2、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;
}
}
}
}
3、aspx页面代码:
4、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;
}
}
}