• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    asp.net 无刷新分页实例代码

    数据类代码:

    复制代码 代码如下:

    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  ListT> DataTableToEntityListT>(DataTable dt)
            {
                ListT> entiyList = new ListT>();

                Type entityType = typeof(T);
                PropertyInfo[] entityProperties = entityType.GetProperties();

                foreach (DataRow row in dt.Rows)
                {
                    T entity = Activator.CreateInstanceT>();

                    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);
                    IListRegisterModel> data = ModelConvertHelperRegisterModel>.ConvertToModel(dt);
                   // IListRegisterModel> data = new UserManageClass().DataTableToEntityListRegisterModel>(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 ListRegisterModel>();               
                    //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;icomments.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 ModelConvertHelperT> where T : new ()
        {
            public static IListT> ConvertToModel(DataTable dt)
            {
             IListT> ts = new ListT>();
            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分页控件,支持多样式多数据绑定
    • 用AJAX实现的无刷新的分页实现代码(asp.net)
    • asp.net jquery无刷新分页插件(jquery.pagination.js)
    • asp.net中利用Jquery+Ajax+Json实现无刷新分页的实例代码
    • asp.net使用AJAX实现无刷新分页
    • asp.net gridview分页:第一页 下一页 1 2 3 4 上一页 最末页
    • asp.net实现简单分页实例
    • asp.net中如何调用sql存储过程实现分页
    • ASP.NET无刷新分页简单实现
    上一篇:asp.net获取URL和IP地址的方法汇总
    下一篇:asp.net判断字符串是否是中文的方法
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

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