日期:2014-05-16  浏览次数:20322 次

Js+C#.Net+AjaxPro实现无刷新分页

配置好AjaxPro,然后什么都不说了 直接贴代码。

?

前台:

<style type="text/css">
    *{ margin:0px; padding:0px; list-style-type:none; font-family:"宋体"; font-size:14px;}
    a{ text-decoration:none;}
    a:hover{ text-decoration:underline;}
    #dataBox{ float:left; height:200px; width:500px; clear:both;}
    #dataBox li{ width:500px; height:30px; line-height:30px;text-overflow:ellipsis; white-space:nowrap;overflow:hidden; cursor:pointer; }
    #dataBox li a{ color:#666;}
    #pagerBar{ float:left; display:inline; width:500px; height:20px; overflow:hidden; clear:both; text-overflow:ellipsis; white-space:nowrap;}
    #pagerBar a{ float:left; display:inline; height:17px; width:20px; border:1px solid #ccc; text-align:center; line-height:17px; cursor:pointer; margin:1px; color:blue;}
    #pagerBar a.outlink{ background:#fff}
    #pagerBar a.onlink{ background:red;}
    #pagerBar span{ float:left; display:inline; height:17px; width:30px; border:1px solid #ccc; text-align:center; line-height:17px; cursor:pointer; margin:1px; color:Blue;}
</style>
    <div id="dataBox"></div>
    <div id="pagerBar"></div>

?

后台:

protected void Page_Load(object sender, EventArgs e)
    {
        AjaxPro.Utility.RegisterTypeForAjax(typeof(AjaxPagerTest));//注册AjaxPro
    }
    [AjaxPro.AjaxMethod]
    public IList<Post> GetAllPostByPage(int pageSize, int pageIndex)//分页获取数据
    {
        int pastCount = pageIndex * pageSize;
        string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
        SqlConnection conn = new SqlConnection(connstr);
        SqlCommand comm = new SqlCommand("select top(" + pageSize + ")* from Post where PostID not in (select top(" + pastCount + ") PostID from Post where PostUserID=13) and PostUserID=13", conn);
        SqlDataAdapter da = new SqlDataAdapter(comm.CommandText, conn);
        try
        {
            DataTable dtData = new DataTable();
            da.Fill(dtData);
            IList<Post> lis = new List<Post>();
            foreach (DataRow dr in dtData.Rows)
            {
                Post post = new Post();
                post.PostTypeName = dr["PostTypeName"].ToString();
                post.PostTitle = dr["PostTitle"].ToString();
                post.PostUserName = dr["PostUserName"].ToString();
                post.PostDate = Convert.ToDateTime(dr["PostDate"]);
                post.Hits = Convert.ToInt32(dr["Hits"]);
                lis.Add(post);
            }
            return lis;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    [AjaxPro.AjaxMethod]
    public int GetPostCount()//获取数据总数
    {
        string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
        SqlConnection conn = new SqlConnection(connstr);
        SqlCommand comm = new SqlCommand("select count(*) from Post where PostUserID=13", conn);
        try
        {
            conn.Open();
            int postCount = Convert.ToInt32(comm.ExecuteScalar());
            conn.Close();
            return postCount;
        }
        catch (Exception)
        {
            throw;
        }
    }

?

?前台:

<script type="text/javascript">
        var dataBox = document.getElementById('dataBox');//数据显示容器
        var pagerBar = document.getElementById('pagerBar');//页码条
        var pageSize = 6; //每页记录数
        var totalSize = getPostCount();//记录总数
        var pageLen = getPageLen();//页数
        var onIndex = 0;//选择页的序号
        //获得记录总数
        function getPostCount() {
            return AjaxPagerTest.GetPostCount().value;
        }
        //获得页数
        function getPageLen() {
            if (totalSize % pageSize == 0)
                return totalSize / pageSize;
            else
                return M