好久不用webform了,这次做的项目又要使用webform(微软推荐使用的是mvc),闲着没事儿干了的时候写个分页控件出来,废话不多说直接上代码
[ DefaultEvent("Paging"), DefaultProperty("Text"), AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), ToolboxData("<{0}:Pager runat=\"server\"></{0}:Pager>") ] public class Pager : CompositeControl { private LinkButton _lbtnPrevious; private LinkButton _lbtnNext; private DropDownList _ddlPageItem; private static readonly object EventPagingKey = new object(); [Bindable(true)] [Category("Appearance")] [DefaultValue("")] [Localizable(true)] public string Text { get { string s = (string) ViewState["Text"]; return (s ?? string.Empty); } set { ViewState["Text"] = value; } } [Category("Appearance")] [DefaultValue(10)] [Localizable(true)] public int PageSize { get { string s = (string) ViewState["PageSize"]; int pageSize = 0; int.TryParse(s, out pageSize); return pageSize; } set { ViewState["PageSize"] = value.ToString(); } } [Category("Appearance")] [DefaultValue(1)] [Localizable(true)] public int CurrentPageIndex { get { string s = (string) ViewState["CurrentPageIndex"]; int currentPageIndex = 0; int.TryParse(s, out currentPageIndex); if (currentPageIndex < 1) currentPageIndex = 1; return currentPageIndex; } set { ViewState["CurrentPageIndex"] = value.ToString(); } } [Category("Appearance")] [DefaultValue(0)] [Localizable(true)] public int RecordCount { get { string s = (string) ViewState["RecordCount"]; int recordCount = 0; int.TryParse(s, out recordCount); return recordCount; } set { ViewState["RecordCount"] = value.ToString(); } } [Category("Appearance")] [DefaultValue(0)] [Localizable(true)] public int PageCount { get { string s = (string) ViewState["PageCount"]; int pageCount = 0; int.TryParse(s, out pageCount); return pageCount; } private set { ViewState["PageCount"] = value.ToString(); } } private void BuildChildControls() { if (PageSize > 0) { PageCount = RecordCount / PageSize; if (RecordCount % PageSize != 0) PageCount++; } _lbtnPrevious = new LinkButton { Text = "上一页", ID = "lbtnPrevious", Enabled = (CurrentPageIndex > 1) }; _lbtnPrevious.Click += _lbtnPrevious_Click; this.Controls.Add(_lbtnPrevious); _ddlPageItem = new DropDownList { ID = "ddlPageItem", AutoPostBack = true }; _ddlPageItem.SelectedIndexChanged += _ddlPageItem_SelectedIndexChanged; for (var i = 1; i <= PageCount; i++) { _ddlPageItem.Items.Add(new ListItem(string.Format("第{0}页", i), i.ToString())); } this.Controls.Add(_ddlPageItem); _lbtnNext = new LinkButton { Text = "下一页", ID = "lbtnNext", Enabled = (CurrentPageIndex < PageCount) }; _lbtnNext.Click += _lbtnNext_Click; this.Controls.Add(_lbtnNext); } protected override void CreateChildControls() { this.Controls.Clear(); ClearChildControlState(); BuildChildControls(); } private void _ddlPageItem_SelectedIndexChanged(object sender, EventArgs e) { int pageIndex; int.TryParse(_ddlPageItem.SelectedItem.Value, out pageIndex); CurrentPageIndex = pageIndex; OnPaging(e); } public event EventHandler Paging { add { this.Events.AddHandler(EventPagingKey, value); } remove { this.Events.RemoveHandler(EventPagingKey, value); } } protected virtual void OnPaging(EventArgs e) { var submitHandler = (EventHandler) Events[EventPagingKey]; if (submitHandler != null) { submitHandler(this, e); } _lbtnPrevious.Enabled = CurrentPageIndex > 1; _lbtnNext.Enabled = CurrentPageIndex < PageCount; _ddlPageItem.SelectedValue = CurrentPageIndex.ToString(); } private void _lbtnPrevious_Click(object sender, EventArgs e) { if (CurrentPageIndex > 1) { CurrentPageIndex -= 1; OnPaging(e); } else { _lbtnPrevious.Enabled = false; } } private void _lbtnNext_Click(object sender, EventArgs e) { if (CurrentPageIndex < PageCount) { CurrentPageIndex += 1; OnPaging(e); } else { _lbtnNext.Enabled = false; } } protected override void Render(HtmlTextWriter writer) { base.Render(writer); } }
至于怎么用我想广大同行们应该比我更清楚,欢迎改进
时间: 2024-11-09 22:33:47