效果图:http://www.ukei.cn/ob1.html 代码这里下载/Files/genson/AjaxPager.rar l准备工作: 
    - jQuery,到http://www.jquery.com下载。
    
 - 分页的存储过程 准备工作:http://www.cnblogs.com/genson/archive/2006/01/17/318882.html 这里可以找到。
    
 - 分页用户控件,首先我们先写一个基类继承 UserControl,主要是分页的属性和一个静态方法,代码如下
    
 using System;
  using System.Data;
  using System.Configuration;
  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 System.Collections;
  using System.Text;
  using System.Collections.Specialized;
  
  
   /**//// <summary>
  /// Summary description for UserControlBase
  /// </summary> public abstract class DataUserControlBase : UserControl
    {
      public int recordCount = 0;
  
  
      public abstract void BindData();
  
      protected override void OnLoad(EventArgs e)
        {
          
          string p = Request.QueryString["p"];
          if (!string.IsNullOrEmpty(p))
            {
              PageIndex = Convert.ToInt32(p);
  
          }
          else
            {
              PageIndex = 1;
             
          }
  
          BindData();
  
          if (!Page.ClientScript.IsClientScriptIncludeRegistered(typeof(Page), "PagerCheck"))
            {
              Page.ClientScript.RegisterClientScriptInclude(typeof(Page), "PagerCheck", ResolveClientUrl("~/js/PagerCheck.js"));
          }
  
          base.OnLoad(e);
      }
      
  
  
      private object[] urlVariantParameters;
       /**//// <summary>
      /// 页面传递多个参数的时候使用,这个要跟UrlParamers替换的数组一致,当参数只有一个的时候,可以省略    
      /// </summary>
      public object[] UrlVariantParameters
        {
           get  { return urlVariantParameters; }
           set  { urlVariantParameters = value; }
      }
  
      private string strWhere = string.Empty;
      public string StrWhere
        {
          get
            {
              return strWhere;
          }
          set
            {
              strWhere = value;
          }
      }
  
      private bool orderType = true;
      public bool OrderType
        {
           get  { return orderType; }
           set  { orderType = value; }
      }
  
      private string fldName = "DateCreated";
      public string FldName
        {
           get  { return fldName; }
           set  { fldName = value; }
      }
  
      private int _recordCount;
      public virtual int RecordCount
        {
          get
            {
              return _recordCount;
          }
          set
            {
              _recordCount = value;
          }
      }
  
  
      private int pageIndex = 1;
      public virtual int PageIndex
        {
          get
            {
              return pageIndex;
          }
          set
            {
              pageIndex = value;
          }
      }
  
  
  
      public virtual int PageCount
        {
          get
            {
              return RecordCount % PageSize == 0 ? RecordCount / PageSize : RecordCount / PageSize + 1;
          }
      }
  
  
      private string urlParamers;
       /**//// <summary>
      /// 指通过页面传递参数的地址:如UrlParamers = "~/ob{0}.html";
      /// </summary>
      public virtual string UrlParamers
        {
          get
            {
              return urlParamers;
          }
          set
            {
              urlParamers = value;
          }
      }
  
  
      private int pageSize = 10;
      public virtual int PageSize
        {
          get
            {
              return pageSize;
          }
          set
            {
              pageSize = value;
          }
      }
  
      private bool ajaxEnabled = true;
       /**//// <summary>
      /// 是否启用Ajax效果分页,默认启用
      /// </summary>
      public virtual bool AjaxEnabled
        {
          get
            {
              return ajaxEnabled;
          }
          set
            {
              ajaxEnabled = value;
          }
      }
  
  
      public static void SetProperty(DataUserControlBase pager, DataUserControlBase b)
        {
          b.RecordCount = b.recordCount;
          pager.RecordCount = b.RecordCount;
          pager.PageSize = b.PageSize;
          pager.PageIndex = b.PageIndex;
          pager.UrlParamers = b.UrlParamers;
          pager.AjaxEnabled = b.AjaxEnabled;
          pager.UrlVariantParameters = b.UrlVariantParameters;      
      }
  
  
     
  
  }
   
     - 然后我们再新增一个ascx文件,叫Pager.ascx吧!继承上面定义的DataUserControlBase,代码如下。
    
  <% @ Control Language="C#" AutoEventWireup="true" CodeFile="Pager.ascx.cs" Inherits="UC_Pager" %>
  <!--这里开始输出页码条-->
  <DIV class="pagebar">
  <asp:PlaceHolder runat=server ID=ph />
  </DIV>
  <!--这里结束页码的输出-->
  
  
  
   
      
  using System;
  using System.Data;
  using System.Configuration;
  using System.Collections;
  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 Obj.DAL;
  
  public partial class UC_Pager : DataUserControlBase
    {
      protected void Page_Load(object sender, EventArgs e)
        {
          GeneratePagerLink(ph);
         
      }
  
    
    
  
      public virtual void GeneratePagerLink(Control container)
        {
        
          int max = PagerHelper.MaxPageIndex(PageIndex);
          int min = PagerHelper.MinPageIndex(PageIndex);
  
          //if (string.IsNullOrEmpty(VirtualCurrentPageUrl))
          //{
          //    throw new Exception("VirtualCurrentPageUrl is  null");
          //}
          if (PageCount <= max) max = PageCount;
  
          if (PageCount == 1) return;
  
  
          if (min != 1) //Prev
            {
              HyperLink hyprev = new HyperLink();
              hyprev.Text = "<< ";
              if (UrlVariantParameters == null)
                {
                  hyprev.NavigateUrl = string.Format(UrlParamers, min - 1);
                  
              }
              else
                {
                  UrlVariantParameters[0] = min - 1;
                  hyprev.NavigateUrl = string.Format(UrlParamers, UrlVariantParameters);
              }
              ConvertAnchor(hyprev, min-1);
              container.Controls.Add(hyprev);          
          }
  
          for (int i = min; i <= max; i++)
            {
              HyperLink hy = new HyperLink();
              if (UrlVariantParameters == null)
                {
                  hy.NavigateUrl = string.Format(UrlParamers, i);
              }
              else
                {
                  UrlVariantParameters[0] = i;
                  hy.NavigateUrl = string.Format(UrlParamers,  UrlVariantParameters);
              }
              hy.Text = i.ToString();
              if (PageIndex == i)
                  hy.NavigateUrl = string.Empty;
              else
                  ConvertAnchor(hy, i);
            
              container.Controls.Add(hy);
              
            
          }
  
          if (PageCount > max)  //Next
            {
              HyperLink hynext = new HyperLink();
              hynext.Text = " >>";
              if (UrlVariantParameters == null)
                {
                  hynext.NavigateUrl = string.Format(UrlParamers, max + 1);
              }
              else
                {
                  UrlVariantParameters[0] = max + 1;
                  hynext.NavigateUrl = string.Format(UrlParamers,  UrlVariantParameters);
              }
                ConvertAnchor(hynext,max+1);
              container.Controls.Add(hynext);           
          }
      }
  
  
  
      private void ConvertAnchor(HyperLink hy,int p)
        {
          if (AjaxEnabled)
            {
              hy.Attributes.Add("onclick", hy.NavigateUrl);
              hy.NavigateUrl = "#" + hy.NavigateUrl;
          }
          
      }
  
  
      public virtual Control Container
        {
          get
            {
              return ph;
          }
          
      }
  
  
  
      public override void BindData()
        {
          
      }
  
      protected override void OnPreRender(EventArgs e)
        {
          if (RecordCount == 0)
            {
              //Container.Controls.Add(new LiteralControl("<p style=text-align:center;>没有数据</p>"));
          }
          base.OnPreRender(e);
      }
  }
   
     
 
当然,少不了分页帮助类PagerHelper,代码如下:这里使用了MicroSoft.Application.Data那个SqlHelper.cs文件
   
  using System;
  using System.Data;
  using System.Configuration;
  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 System.Data.SqlClient;
  using Microsoft.ApplicationBlocks.Data;
  namespace Obj.DAL
    {
       /**//// <summary>
      /// Summary description for PageHelper
      /// </summary>
  
       /**//**/
       /**//// <summary> 
      /// 分页类PagerHelper 的摘要说明。 
      /// </summary> 
      public class PagerHelper
        {
          private string connectionString;
  
           /**//// <summary>
          /// 得到记录集的构造函数
          /// </summary>
          /// <param name="tblname"></param>
          /// <param name="sortname"></param>
          /// <param name="docount"></param>
          /// <param name="connectionString"></param>
          public PagerHelper(string tblname, string sortname, bool docount, string connectionString)
            {
              this.tblName = tblname;
              this.fldName = sortname;
              this.connectionString = connectionString;
              this.docount = docount;
          }
  
           /**//// <summary>
          /// 所有参数都有
          /// </summary>
          /// <param name="tblname"></param>
          /// <param name="docount"></param>
          /// <param name="strGetFields"></param>
          /// <param name="fldName"></param>
          /// <param name="pagesize"></param>
          /// <param name="pageindex"></param>
          /// <param name="ordertype"></param>
          /// <param name="strwhere"></param>
          /// <param name="connectionString"></param>
          public PagerHelper(string tblname, bool docount,
              string strGetFields, string fldName, int pagesize,
              int pageindex, bool ordertype, string strwhere, string connectionString
              )
            {
              this.tblName = tblname;
              this.docount = docount;
              this.strGetFields = strGetFields;
              this.fldName = fldName;
              this.pagesize = pagesize;
              this.pageindex = pageindex;
              this.ordertype = ordertype;
              this.strwhere = strwhere;
              this.connectionString = connectionString;
             
  
          }
  
  
           /**//**/
           /**//// <summary> 
          /// 得到记录集的构造函数 
          /// </summary> 
          /// <param name="tblname"></param> 
          /// <param name="strwhere"></param> 
          /// <param name="connectionString"></param> 
          public PagerHelper(string tblname, string strwhere, string connectionString)
            {
              this.tblName = tblname;
              this.strwhere = strwhere;
              this.docount = true;
              this.connectionString = connectionString;
          }
  
          private string tblName;
          public string TblName
            {
               get  { return tblName; }
               set  { tblName = value; }
          }
  
          private string strGetFields = "*";
          public string StrGetFields
            {
               get  { return strGetFields; }
               set  { strGetFields = value; }
          }
  
          private string fldName = string.Empty;
          public string FldName
            {
               get  { return fldName; }
               set  { fldName = value; }
          }
  
  
  
          private int pagesize = 10;
          public int PageSize
            {
               get  { return pagesize; }
               set  { pagesize = value; }
          }
  
          private int pageindex = 1;
          public int PageIndex
            {
               get  { return pageindex; }
               set  { pageindex = value; }
          }
  
  
          private bool docount = false;
          public bool DoCount
            {
               get  { return docount; }
               set  { docount = value; }
          }
  
          private bool ordertype = false;
          public bool OrderType
            {
               get  { return ordertype; }
               set  { ordertype = value; }
          }
  
          private string strwhere = string.Empty;
          public string StrWhere
            {
               get  { return strwhere; }
               set  { strwhere = value; }
          }
  
  
  
  
  
  
          public IDataReader GetDataReader()
            {
  
              if (this.docount)
                {
                  throw new ArgumentException("要返回记录集,DoCount属性一定为false");
              }
  
  
  
              //    System.Web.HttpContext.Current.Response.Write(pageindex); 
  
              return SqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, "Pagination",
                  new SqlParameter("@tblName", this.tblName),
                  new SqlParameter("@strGetFields", this.strGetFields),
                  new SqlParameter("@fldName", this.fldName),
                  new SqlParameter("@PageSize", this.pagesize),
                  new SqlParameter("@PageIndex", this.pageindex),
                  new SqlParameter("@doCount", this.docount),
                  new SqlParameter("@OrderType", this.ordertype),
                  new SqlParameter("@strWhere", this.strwhere)
                  );
          }
  
          public DataSet GetDataSet()
            {
              if (this.docount)
                {
                  throw new ArgumentException("要返回记录集,DoCount属性一定为false");
              }
  
              return SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, "Pagination",
                  new SqlParameter("@tblName", this.tblName),
                  new SqlParameter("@strGetFields", this.strGetFields),
                  new SqlParameter("@fldName", this.fldName),
                  new SqlParameter("@PageSize", this.pagesize),
                  new SqlParameter("@PageIndex", this.pageindex),
                  new SqlParameter("@doCount", this.docount),
                  new SqlParameter("@OrderType", this.ordertype),
                  new SqlParameter("@strWhere", this.strwhere)
                  );
          }
  
  
          public int GetCount()
            {
              if (!this.docount)
                {
                  throw new ArgumentException("要返回总数统计,DoCount属性一定为true");
              }
  
  
  
              return (int)SqlHelper.ExecuteScalar(connectionString, CommandType.StoredProcedure, "Pagination",
                  new SqlParameter("@tblName", this.tblName),
                  new SqlParameter("@strGetFields", this.strGetFields),
                  new SqlParameter("@fldName", this.fldName),
                  new SqlParameter("@PageSize", this.pagesize),
                  new SqlParameter("@PageIndex", this.pageindex),
                  new SqlParameter("@doCount", this.docount),
                  new SqlParameter("@OrderType", this.ordertype),
                  new SqlParameter("@strWhere", this.strwhere)
                  );
          }
  
  
          public static int MaxPageIndex(int pageindex)
            {
              if (pageindex < 10)
                  return 10;
              string numstr = pageindex.ToString();
              numstr = numstr.Substring(0, numstr.Length - 1);
            
            
              return Convert.ToInt32(numstr + "0") + 10;
  
          }
  
          public static int MinPageIndex(int pageindex)
            {
              if (pageindex < 10)
                  return 1;
              string numstr = pageindex.ToString();
              numstr = numstr.Substring(0, numstr.Length - 1);
              return Convert.ToInt32(numstr + "0");
          }
  
      }
  
  }
这一切准备好之后,我们就可以对数据进行分页程序开发了。 这篇文章也许写得不是很好,同样代码也有很多可以修改的地方,代码我已经测试过,兼容ie和ff2.0。如果有问题,欢迎回帖,也可以 发信息给我。   
  
 
         
        
            发表于 
 2007-06-05 17:14 
欢欢 
阅读( 559) 
评论() 
 
 收藏 
举报
         
     
      
	
	
    
    
 
 
 | 
                 
             
         | 
        
    
Feedback
我做了一个用自带控件的,你可以看看,交流一下。 回复 更多评论
不过地址栏中出现:http://localhost:1238/WebSite2/Default.aspx#javascript:showPagerUserInfo(9,'container');
#javascript:showPagerUserInfo(9,'container');这段东东,不太好。 回复 更多评论
看了一下,有不同点:你的是用post提交的,我的是用get,自带那个ScriptManager应该来说实现Ajax是比较方便的,但由于我不是很熟悉,所以也说不出那个好,但如果对js熟悉的话,用这个还是比较方便的。 回复 更多评论
这个是浏览器点后退的时候返回当前页,而不是返回第一页,抓虾也就是这样的 回复 更多评论