| 网站首页 | 文章中心 | 电子书下载 | 矢量图库 | 视频教程 | 素材下载 | 程序代码下载 | JS代码 | 论坛 | 
龙腾教程网  
常用软件类:
|杀毒安全 |联络聊天 |网络软件 |多媒体类 |系统工具 |图形图像 |系统工具 |应用软件 |行业软件
开发设计类:
|动画制作 |图像处理 |3D设计 |操作系统 |站长学院 |网络相关 |WEB设计 |数据库类 |程序开发
 
  您当前位置:您现在的位置: 龙腾软件教程网 >> 文章中心 >> WEB设计 >> AJAX >> 编程实列 >> 文章正文>>>>>> 
ajax实现分页控件代码
/**************************
  创建XMLHttpRequest对象
  
**************************/
function CreateRequest()
{
    var xmlObj = null;
    try 
    {
        xmlObj = new XMLHttpRequest();
    }
    catch(e)
    {
        try
        {
            xmlObj = new ActiveXObject("MSXML2.XMLHTTP");
        }
        catch(e2)
        {
            try
            {
                xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e3)
            {
                xmlObj = null;
            }
        }
    }
    
    if ( !xmlObj && typeof XMLHttpRequest != "undefined" ) 
    { 
        xmlObj=new XMLHttpRequest();
    } 
    return xmlObj;
}
function Poress(weburl,objID) 
...{
    var objP = document.getElementById(objID) //得到父控件 
    var xmlhttp = CreateRequest(); //初始化个xmlhttp对象 
    xmlhttp.open("get",weburl,true); //提交数据
    xmlhttp.onreadystatechange=function() 
    ...{ 
        if(xmlhttp.readyState==4)//4代表成功返回数据 
        ...{ 
            var result = xmlhttp.responseText;//得到服务器返回的数据 
            if(result.indexOf("##Err##")>=0)
            ...{
                page=result.substr(0,result.indexOf("##Err##"));
                var regExp=/page=([-0-9]*)/i;
                weburl=chkUrl(weburl,regExp,"page="+page);
                Poress(weburl,objID);
            }
            else
            objP.innerHTML=result;
        }
    };
    xmlhttp.send(null); //发送数据,请注意顺序和参数,参数一定为null或者"" 
}
function changeUrl(weburl,divID)
...{
    var txtPage=document.getElementById("txtPage");
    if(txtPage!=null)
    ...{
        var page=txtPage.value;
        var regExp=/page=([-0-9]*)/i;
        weburl=chkUrl(weburl,regExp,"page="+page);
    }
    if(divID.length==0)
        location.href=weburl;
    else
        Process(weburl,divID);
}

function changePage(page)
...{
    var txtPage=document.getElementById("txtPage");
    txtPage.value=page;
}

function chkUrl(url,regExp,parma)
...{
    if(url.indexOf("?")<0)
        url+="?"+parma;
    else if(regExp.test(url))
    ...{
        url=url.replace(regExp,parma);
    }
    else
        url+="&"+parma;
    return url;
}

function changeOrderUrl(weburl,divID,ascValue)
...{
    var drpSortBy=document.getElementById("drpSortBy");
    var btnOrder=document.getElementById("btnOrder");

    var regExp=/SortBy=([^&]*)/i;
    weburl=chkUrl(weburl,regExp,"SortBy="+drpSortBy.value);
    
    if(btnOrder.value==ascValue)
        strSortOrder="DESC"
    else
        strSortOrder="ASC"
        
    regExp=/SortOrder=([^&]*)/i;
    weburl=chkUrl(weburl,regExp,"SortOrder="+strSortOrder);

    if(divID.length==0)
        location.href=weburl;
    else
        Process(weburl,divID);
}

function changeOrder()
...{
    var btnOrder=document.getElementById("btnOrder");
    btnOrder.value=’’;
}

ascx
请自己设置载入JS文件

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PageControl.ascx.cs" Inherits="DonDear.Web.UserControls.PageControl" %>
<asp:Literal ID="ltPage" runat="server"></asp:Literal>
cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Text.RegularExpressions;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DonDear.Utility;

namespace DonDear.Web.UserControls
...{
    public partial class PageControl : System.Web.UI.UserControl
    ...{
        public string mStyleClass = "", mSelectedStyleClass = "", mInputStyle = "", mButtonStyle="", mFirstPage = "首页", mLastPage = "尾页", mPrePage = "上页", mNextPage = "下页", mGoPage = "Go";
        public int mPage = 0, mPageSize = 20, mTotalCount = 121, mShowCount = 5;
        public bool mShowButton = false,mShowFix=true;
        public string mContentID = "";

        public string ContentID
        ...{
            set ...{ this.mContentID = value; }
        }

        public string StyleClass
        ...{
            set ...{ this.mStyleClass = value; }
        }

        public string SelectedStyleClass
        ...{
            set ...{ this.mSelectedStyleClass = value; }
        }
        public string InputStyle
        ...{
            set ...{ this.mInputStyle = value; }
        }

        public string ButtonStyle
        ...{
            set ...{ this.mButtonStyle = value; }
        }

        public string FirstPage
        ...{
            set ...{ this.mFirstPage = value; }
        }

        public string LastPage
        ...{
            set ...{ this.mLastPage = value; }
        }

        public string PrePage
        ...{
            set ...{ this.mPrePage = value; }
        }

        public string NextPage
        ...{
            set ...{ this.mNextPage = value; }
        }

        public string GoPage
        ...{
            set ...{ this.mGoPage = value; }
        }

        public int ToPage
        ...{
            set ...{ this.mPage = value; }
            get ...{ return this.mPage; }
        }

        public int PageSize
        ...{
            set ...{ this.mPageSize = value; }
            get ...{ return this.mPageSize; }
        }

        public int TotalCount
        ...{
            get ...{ return this.mTotalCount; }
            set ...{ this.mTotalCount = value; }
        }

        public int ShowCount
        ...{
            set ...{ this.mShowCount = value; }
        }

        public bool ShowButton
        ...{
            set ...{ this.mShowButton = value; }
        }

        public bool ShowFix
        ...{
            set ...{ this.mShowFix = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        ...{
            string webUrl = this.Request.Url.ToString();
            Regex regExp = new Regex("Page=([-0-9]*)", RegexOptions.IgnoreCase);
            if (!Page.IsPostBack)
            ...{
                int pageCount = 1;

                if (this.mPageSize > 0)
                ...{
                    if ((mTotalCount % mPageSize) == 0)
                        pageCount = mTotalCount / mPageSize;
                    else
                        pageCount = (mTotalCount / mPageSize) + 1;
                }

                StringBuilder sb = new StringBuilder();
                
                if (Request.QueryString["Page"] != null && this.mPage<1)
                    this.mPage = Convert.ToInt32(Request.QueryString["Page"]);
                if (this.mPage < 1)
                ...{
                    this.mPage = 1;
                    webUrl = StrHelper.ReplaceUrl(webUrl, regExp, "Page=1");
                    Response.Redirect(webUrl);
                }
                if (this.mPage > pageCount)
                ...{
                    this.mPage = pageCount;
                    webUrl = StrHelper.ReplaceUrl(webUrl, regExp, "Page=" + pageCount.ToString());
                    Response.Redirect(webUrl);
                }
                else
                ...{
                    string strScript = "javascript:changeUrl(’" + webUrl + "’,’" + this.mContentID + "’);";
                    string strClick = "";
                    //首页
                    if (this.mPage <= 1)
                        sb.Append("<a " + mStyleClass + ">" + mFirstPage + "</a> <a " + mStyleClass + ">" + mPrePage + "</a> ");
                    else
                    ...{
                        strClick = "changePage(1);" + strScript;
                        sb.Append("<a href=’javascript:;’ onclick="" + strClick + "" " + mStyleClass + ">" + mFirstPage + "</a> ");
                        strClick = "changePage(" + (this.mPage - 1) + ");" + strScript;
                        sb.Append("<a href=’javascript:;’ onclick="" + strClick + "" " + mStyleClass + ">" + mPrePage + "</a> ");
                    }


                    int befPageCount = 0, nextPageCount = pageCount / 2, midPage = pageCount / 2, fromPage = 1, toPage = pageCount;

                    if (this.mShowCount > pageCount)
                        this.mShowCount = pageCount;

                    if ((this.mShowCount % 2) == 0)
                        midPage = this.mShowCount / 2;
                    else
                        midPage = (this.mShowCount / 2) + 1;
                    befPageCount = midPage - 1;
                    nextPageCount = this.mShowCount - midPage;
                    //}

                    if (this.mPage > midPage)
                    ...{
                        fromPage = 1 + (this.mPage - midPage);
                        toPage = this.mShowCount + this.mPage - midPage;
                    }
                    else
                        toPage = this.mShowCount;

                    if (this.mShowCount + (this.mPage - midPage) >= pageCount)
                    ...{
                        fromPage = pageCount - this.mShowCount + 1;
                        toPage = pageCount;
                    }

                    if (fromPage > 1 && this.mShowFix)
                        sb.Append("<a " + mStyleClass + ">...</a> ");

                    for (int i = fromPage; i <= toPage; i++)
                    ...{
                        if (i == this.mPage)
                            sb.Append("<a " + this.mSelectedStyleClass + ">" + i + "</a> ");
                        else
                        ...{
                            strClick = "changePage(" + i + ");" + strScript;
                            sb.Append("<a href=’javascript:;’ onclick="" + strClick + "" " + this.mStyleClass + ">" + i + "</a> ");
                        }
                    }

                    if (toPage != pageCount && this.mShowFix)
                        sb.Append("<a " + mStyleClass + ">...</a> ");

                    if (this.mPage >= pageCount)
                        sb.Append("<a " + mStyleClass + ">" + this.mNextPage + "</a> <a " + mStyleClass + ">" + this.mLastPage + "</a> ");
                    else
                    ...{
                        strClick = "changePage(" + (this.mPage + 1) + ");" + strScript;
                        sb.Append("<a href=’javascript:;’ onclick="" + strClick + "" " + mStyleClass + ">" + mNextPage + "</a> ");
                        strClick = "changePage(" + pageCount + ");" + strScript;
                        sb.Append("<a href=’javascript:;’ onclick="" + strClick + "" " + mStyleClass + ">" + mLastPage + "</a> ");
                    }
                    string strStyle = "";
                    if (!this.mShowButton)
                    ...{
                        strStyle = "display:none;";
                    }
                    sb.Append("<input size=’3px’ type=’text’ id=’txtPage’ value=’" + this.mPage + "’ " + mInputStyle + " style=’width: 23px;" + strStyle + "’> <input type=’button’ id=’btnGo’ onclick="" + strScript + "" " + mButtonStyle + " value=’" + mGoPage + "’ style=’" + strStyle + "’ /> ");
                }
                this.ltPage.Text = sb.ToString();
            }
        }
    }
}
上一篇:
  • 上一篇文章:
  • 下一篇
  • 下一篇文章: 没有了
  • 收藏此文到百度搜藏 百度搜藏| 新浪VIvi| 365key| Younote| 博采中心| 你好BLOG| 亿友网摘| 和讯网摘|
    相关文章    
    AJAX联手SOA 新一代Web2.0应用程序
    ASP.NET AJAX实现网页拖放功能的方法总结
    利用ASP.NET AJAX框架开发幻灯片播放网页
    Ajax技术适用和不适用场景简析
    AJAX实现web页面中级联菜单的设计
    AJAX学习基础 -理解JavaScript函数
    详谈基于JSON的高级AJAX开发技术
    AJAX推送与拉取方式的比较
    Mashups+AJAX打造全新Web开发
    AJAX制作目录树
     
     
     
    最新文章
    普通文章 ajax实现分页控件代码最新文章
    普通文章 javascript动态更改DropDownList最新文章
    普通文章 javascript 的window.open()的参最新文章
    普通文章 photoshop绘制森林中的精灵最新文章
    普通文章 photoshop设计卡通饼干字体最新文章
    普通文章 SnagIt批量捕获网络图像技巧最新文章
    普通文章 迅雷文件管理实用招数最新文章
    普通文章 基于VLAN技术的校园一卡通管理最新文章
    普通文章 图解校园网局域网组网方案剖析最新文章
    普通文章 小心路由器故障威胁你的网络最新文章
     
    热门文章
    推荐文章 Photoshop绘制卡通动漫娃娃最新文章
    推荐文章 Photoshop绘制逼真绚丽的金鱼
     
     
    设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 

    版权所有2006-2008 龙腾教程网