欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 无刷新分页实例代码

程序员文章站 2024-03-02 20:19:16
数据类代码: 复制代码 代码如下:using system;using system.collections.generic;using system.linq;usin...

数据类代码:

复制代码 代码如下:

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;
        }

   
    }
}