分页控件

JS

/**************************
  创建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:;' οnclick="" + strClick + "" + mStyleClass + ">" + mFirstPage + "</a> ");
                        strClick 
= "changePage(" + (this.mPage - 1+ ");" + strScript;
                        sb.Append(
"<a href='javascript:;' οnclick="" + 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:;' οnclick="" + 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:;' οnclick="" + strClick + "" + mStyleClass + ">" + mNextPage + "</a> ");
                        strClick 
= "changePage(" + pageCount + ");" + strScript;
                        sb.Append(
"<a href='javascript:;' οnclick="" + 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' οnclick="" + strScript + "" + mButtonStyle + " value='" + mGoPage + "' style='" + strStyle + "' /> ");
                }

                
this.ltPage.Text = sb.ToString();
            }

        }

    }

}

上次写得太复杂了,重新修改了下,感觉很完美,嘿嘿!!   @ 2007-12-04

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值