效果图: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);
}
}
















































































































































































































































