ASP.NET分页控件   发布时间:2017-09-09 10:47:15

ASP.NET分页控件 效果图

这个在用户控件一用一次

<%@ Register Assembly="Uni2Uni.ERP.Common" Namespace="Uni2Uni.ERP.Common.PageBar"

TagPrefix="cc1" %>

<cc1:PageBar ID="pageBar" runat="server" Mode="Post" />

然后使用那个用户控件

[html]

<uc1:PageBarControl ID="PageBarControl1" runat="server" />

PageBarControl1.PageBar.Click += new EventHandler(PagerChange);

private void PagerChange(object sender, EventArgs e)

{

var queryEntity = ViewState["queryEntity"] as DamagedQueryEntity;

GetPageData(queryEntity, false);

}

private void GetPageData(DamagedQueryEntity queryEntity, bool bFirst = true)

{

if (bFirst)

PageBarControl1.PageBar.CurrentIndex = 1;

int totalCount;

var list = bll.GetDamagedOrderPage(PageBarControl1.PageBar.CurrentIndex, PageBarControl1.PageBar.Size, out totalCount, queryEntity);

this.PageBarControl1.PageBar.Total = totalCount;

this.bindData.DataSource = list;

this.bindData.DataBind();

}

源码

[html]

using System;

using System.Collections.Specialized;

using System.ComponentModel;

using System.Text;

using System.Text.RegularExpressions;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

[assembly: WebResource("Uni2Uni.ERP.Common.PageBar.PageBar.css", "text/css", PerformSubstitution = true)]

namespace Uni2Uni.ERP.Common.PageBar

{

public enum PageBarMode

{

Get,

Post

}

#region - PageBar -

/// <summary>

/// 分页控件

/// </summary>

[ToolboxData("<{0}:PageBar runat=\"server\" />")]

[DefaultEvent("Click")]

[DefaultProperty("Size")]

public sealed class PageBar : WebControl, IPostBackEventHandler

{

#region - Properties -

#region = Total =

/// <summary>

/// 总共有多少记录集合

/// </summary>

/// <value>The total.</value>

[Category("Data"), Description("总共有多少记录集合")]

public int Total

{

get

{

object o = ViewState["Total"];

return o == null ? 0 : (int)o;

}

set { ViewState["Total"] = value; }

}

#endregion

#region = CurrentIndex =

/// <summary>

/// 当前使用的页面

/// </summary>

/// <value>The index of the current.</value>

[Category("Data"), Description("当前使用的页面")]

public int CurrentIndex

{

get

{

object o = ViewState["CurrentIndex"];

int _currentindex = ((o == null) ? 0 : (int)o);

if (_currentindex == 0)

{

_currentindex = 1;

if (string.IsNullOrEmpty(UrlFormatString) == false)

{

string url = Page.Request.Url.ToString();

string urlformat = string.Format(UrlFormatString, @"(\d+)");

urlformat = urlformat.Replace("?", "\\?");

urlformat = Regex.Replace(urlformat, "#.+$", string.Empty);

Match m = Regex.Match(url, urlformat, RegexOptions.IgnoreCase);

if (m.Success)

{

string value = m.Groups[1].Value;

if (string.IsNullOrEmpty(value) == false)

{

_currentindex = int.Parse(value);

}

}

}

else

{

if (Page.Request.QueryString[QueryStringKey] != null)

{

int.TryParse(Page.Request.QueryString[QueryStringKey], out _currentindex);

}

}

}

return _currentindex;

}

set { ViewState["CurrentIndex"] = value; }

}

#endregion

#region = Size =

/// <summary>

/// 每个页面显示多少记录集合

/// </summary>

/// <value>The size.</value>

[Category("Data"), Description("每个页面显示多少记录集合")]

public int Size

{

get

{

object o = ViewState["Size"];

return o == null ? 10 : (int)o;

}

set { ViewState["Size"] = value; }

}

#endregion

#region = Displaysize =

/// <summary>

/// 显示页脚,控件显示多少页面数

/// </summary>

/// <value>The displaysize.</value>

[Category("Data"), Description("控件显示多少页面数")]

public int Displaysize

{

get

{

object o = ViewState["Displaysize"];

return o == null ? 10 : (int)o;

}

set { ViewState["Displaysize"] = value; }

}

#endregion

#region = Unit =

/// <summary>

/// 记录的单位

/// </summary>

/// <value>The unit.</value>

[Category("Appearance"), Description("记录的单位")]

public string Unit

{

get

{

object o = ViewState["Unit"];

return o == null ? string.Empty : (string)o;

}

set { ViewState["Unit"] = value; }

}

#endregion

#region = UrlFormatString =

[

Category("Property"),

Description("获取或设置 URL 显示格式。"),

DefaultValue("")

]

public string UrlFormatString

{

get

{

object o = ViewState["UrlFormatString"];

return o == null ? string.Empty : (string)o;

}

set { ViewState["UrlFormatString"] = value; }

}

#endregion

#region = QueryStringKey =

/// <summary>

/// 传递值是使用的关键字

/// </summary>

/// <value>The query string key.</value>

[Description("传递值是使用的关键字"), DefaultValue("page")]

public string QueryStringKey

{

get

{

object o = ViewState["QueryStringKey"];

return o == null ? "page" : (string)o;

}

set { ViewState["QueryStringKey"] = value; }

}

#endregion

#region = 跳转的页数 =

/// <summary>

/// 跳转的页数

/// </summary>

[Browsable(true)]

[Bindable(true, BindingDirection.TwoWay)]

[DefaultValue("")]

[Localizable(true)]

[PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]

[Description("跳转的页数")]

public string Text

{

get

{

return ViewState["Text"] == null ? "" : ViewState["Text"].ToString();

}

set

{

ViewState["Text"] = value;

}

}

#endregion

#region = Format =

/// <summary>

/// 数字格式化

/// </summary>

/// <value>The format.</value>

[Description("数字格式化"), DefaultValue("")]

public string Format

{

get

{

object o = ViewState["Format"];

return o == null ? string.Empty : (string)o;

}

set { ViewState["Format"] = value; }

}

#endregion

#region = NextPageImageUrl =

/// <summary>

/// 下一页图片路径

/// </summary>

/// <value>The next page image URL.</value>

[Description("下一页图片路径"), DefaultValue("")]

public string NextPageImageUrl

{

get

{

object o = ViewState["NextPageImageUrl"];

return o == null ? string.Empty : (string)o;

}

set { ViewState["NextPageImageUrl"] = value; }

}

#endregion

#region = LastPageImageUrl =

private string _lastimage = string.Empty;

/// <summary>

/// 上一页图片路径

/// </summary>

/// <value>The last page image URL.</value>

[Description("上一页图片路径"), DefaultValue("")]

public string LastPageImageUrl

{

get

{

object o = ViewState["LastPageImageUrl"];

return o == null ? string.Empty : (string)o;

}

set { ViewState["LastPageImageUrl"] = value; }

}

#endregion

#region = Mode =

[

Category("Property"),

Description("Pagebar的模式"),

DefaultValue("")

]

public PageBarMode Mode

{

get

{

object o = ViewState["Mode"];

return o == null ? PageBarMode.Get : (PageBarMode)o;

}

set { ViewState["Mode"] = value; }

}

#endregion

#region = ValidationGroup =

[Category("Behavior"), DefaultValue(""), Themeable(false), Description("PostBackControl_ValidationGroup")]

public string ValidationGroup

{

get

{

var text1 = (string)ViewState["ValidationGroup"];

if (text1 != null)

{

return text1;

}

return string.Empty;

}

set { ViewState["ValidationGroup"] = value; }

}

#endregion

#region = CausesValidation =

/// <summary>

/// 获取或设置一个值,该值指示在单击 LinkButton 控件时是否执行验证。

/// </summary>

/// <value><c>true</c> if [causes validation]; otherwise, <c>false</c>.</value>

[Category("Behavior"), Themeable(false), Description("Button_CausesValidation"), DefaultValue(true)]

public bool CausesValidation

{

get

{

object obj1 = ViewState["CausesValidation"];

if (obj1 != null)

{

return (bool)obj1;

}

return true;

}

set { ViewState["CausesValidation"] = value; }

}

#endregion

#region = Target =

/// <summary>

/// Gets or sets the target.

/// </summary>

/// <value>The target.</value>

[

Category("Navigation"),

TypeConverter(typeof(TargetConverter)),

DefaultValue(""),

Description("HyperLink_Target")]

public string Target

{

get

{

var text1 = (string)ViewState["Target"];

if (text1 != null)

{

return text1;

}

return "_self";

}

set { ViewState["Target"] = value; }

}

#endregion

#endregion

#region - Event -

#region = OnClick =

private static readonly object EventClick = new object();

[Description("PageBar_OnClick"), Category("Action")]

public event EventHandler Click

{

add { Events.AddHandler(EventClick, value); }

remove { Events.RemoveHandler(EventClick, value); }

}

#endregion

#endregion

#region = OnPreRender =

/// <summary>

/// Raises the <see cref="E:System.Web.UI.Control.PreRender"></see> event.

/// </summary>

/// <param name="e">An <see cref="T:System.EventArgs"></see> object that contains the event data.</param>

protected override void OnPreRender(EventArgs e)

{

base.OnPreRender(e);

string cssKey = "PageBarCss";

if (Page == null || Page.Header == null)

{

return;

}

if (string.IsNullOrEmpty(CssClass))

{

CssClass = "PageBar";

}

if (Page.Header.FindControl(cssKey) == null && CssClass == "PageBar")

{

string css = Page.ClientScript.GetWebResourceUrl(GetType(), "Uni2Uni.ERP.Common.PageBar.PageBar.css");

var hl = new HtmlLink();

hl.ID = cssKey;

hl.Href = css;

hl.Attributes["type"] = "text/css";

hl.Attributes["rel"] = "stylesheet";

//this.Page.Header.Controls.Add(hl);

CssClass = "PageBar";

}

}

#endregion

#region = RenderContents =

/// <summary>

/// Renders the contents of the control to the specified writer. This method is used primarily by control developers.

/// </summary>

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:襄阳SEO http://xiangyang.raoyu.net