<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Pageing1.ascx.cs" Inherits="EB.Mall.Services.UI.Web.Pageing1" %> <style type="text/css"> ul { list-style:none; } ul li { float:left; /* 向左漂移,将竖排变为横排 */ } </style> <div id="divMain" runat="server" style="text-align:center;"> <input type="hidden" runat="server" id="hidPageIndex" value="0"/> <input type="hidden" runat="server" id="hidPageCount" value="0"/> <input type="hidden" runat="server" id="hidToPage" value="0"/> <asp:Repeater ID="rptPageing" runat="server" OnItemDataBound="rptPageing_ItemDataBound"> <HeaderTemplate> <ul> <li> <asp:LinkButton ID="linkFirst" CommandArgument="f" OnCommand="linkFirst_Command" runat="server">首页</asp:LinkButton> </li> <li> <asp:LinkButton ID="linkPrev" CommandArgument="p" OnCommand="linkFirst_Command" runat="server">上一页</asp:LinkButton> </li> </HeaderTemplate> <ItemTemplate> <li> <asp:LinkButton ID="linkItem" CommandArgument="i" OnCommand="linkFirst_Command" CommandName="" runat="server"><%#Container.DataItem %></asp:LinkButton> </li> </ItemTemplate> <FooterTemplate> <li> <asp:LinkButton ID="linkNext" CommandArgument="n" OnCommand="linkFirst_Command" runat="server">下一页</asp:LinkButton> </li> <li> <asp:LinkButton ID="linkLast" CommandArgument="l" OnCommand="linkFirst_Command" runat="server">末页</asp:LinkButton> </li> <li> <asp:Label ID="lblStatistic" runat="server"></asp:Label> </li> <li> 转到第 <asp:TextBox ID="txtToPage" Width="25" Height="12" runat="server"></asp:TextBox>页 <asp:Button ID="btnToPage" runat="server" Text="确定" OnClick="btnToPage_Click" OnClientClick="return onClientClick(this);" /> </li> </ul> </FooterTemplate> </asp:Repeater> </div> <script type="text/javascript"> function onClientClick(obj) { var toPageIndex= obj.previousElementSibling.value; var toPageIndex try { var toPageIndex=parseInt(obj.previousElementSibling.value); var totalCount=parseInt(document.getElementById("<%=hidPageCount.ClientID%>").value ); if(!toPageIndex||toPageIndex<=0||toPageIndex>totalCount) { alert("页码输入错误!"); return false; } } catch(e){} document.getElementById("<%=hidToPage.ClientID%>").value = obj.previousElementSibling.value; return true; } </script>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace EB.Mall.Services.UI.Web { public partial class Pageing1 : System.Web.UI.UserControl { public class PageingEventArgs { public PageingEventArgs(int pageIndex) { this.pageIndex = pageIndex; } private int pageIndex = 0; public int PageIndex { get { return this.pageIndex; } } } public delegate void PageingHandler(object obj, PageingEventArgs e, out int pageCount); public event PageingHandler OnPageing; private int showPageCount = 5; protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack&&this.loadShow) { GoFirstPage(); } } public void GoFirstPage() { PageingMethod(1); } public void GoPage(int pageIndex) { PageingMethod(pageIndex); } private void PageingMethod(int pageIndex) { int pageCount = 0; if (OnPageing != null) { OnPageing(this, new PageingEventArgs(pageIndex), out pageCount); if (pageCount <= 0) { this.divMain.InnerText = "暂无数据"; } else { this.PageIndex = pageIndex; int bs = this.PageIndex % showPageCount; int startIndex = 0; if (bs == 0) { startIndex = this.PageIndex - 4; if (startIndex <= 0) { startIndex = 1; } } else { startIndex = (this.PageIndex / showPageCount) * showPageCount + 1; } int endIndex = startIndex + 4; if (endIndex > pageCount) { endIndex = pageCount; } List<int> itemList = new List<int>(); for (int i = startIndex; i <= endIndex; i++) { itemList.Add(i); } this.PageCount = pageCount; this.rptPageing.DataSource = itemList; this.rptPageing.DataBind(); } } } private bool loadShow = false; /// <summary> /// 是否页面首次进入不点查询就要显示数据 /// </summary> public bool LoadShow { set { loadShow = value; } } /// <summary> /// 当前页 /// </summary> public int PageIndex { get { int pageIndex = 0; int.TryParse(this.hidPageIndex.Value, out pageIndex); return pageIndex; } set { this.hidPageIndex.Value = value.ToString(); } } /// <summary> /// 页数 /// </summary> public int PageCount { get { int pagecount = 0; int.TryParse(this.hidPageCount.Value, out pagecount); return pagecount; } set { this.hidPageCount.Value = value.ToString(); } } protected void rptPageing_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Header) { LinkButton linkFirst = e.Item.FindControl("linkFirst") as LinkButton; LinkButton linkPrev = e.Item.FindControl("linkPrev") as LinkButton; if (PageIndex == 1) { linkFirst.Enabled = false; linkPrev.Enabled = false; } else { linkFirst.Enabled = true; linkPrev.Enabled = true; } } else if (e.Item.ItemType == ListItemType.Footer) { LinkButton linkLast = e.Item.FindControl("linkLast") as LinkButton; LinkButton linkNext = e.Item.FindControl("linkNext") as LinkButton; if (PageIndex == PageCount) { linkLast.Enabled = false; linkNext.Enabled = false; } else { linkLast.Enabled = true; linkNext.Enabled = true; } Label lblStatistic = e.Item.FindControl("lblStatistic") as Label; lblStatistic.Text = string.Format("共{0}页",PageCount); } else if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { LinkButton linkItem = e.Item.FindControl("linkItem") as LinkButton; linkItem.Text = e.Item.DataItem.ToString(); if ((int)e.Item.DataItem == PageIndex) { linkItem.Enabled = false; } else { linkItem.Enabled = true; } } } protected void linkFirst_Command(object sender, CommandEventArgs e) { LinkButton linkb = sender as LinkButton; string type = e.CommandArgument.ToString(); switch (type) { case "f": PageingMethod(1); break; case "p": PageingMethod(this.PageIndex - 1); break; case "n": PageingMethod(this.PageIndex + 1); break; case "l": PageingMethod(this.PageCount); break; case "i": PageingMethod(Convert.ToInt32(linkb.Text)); break; } } protected void btnToPage_Click(object sender, EventArgs e) { if(this.rptPageing.Items.Count<=0) { return; } int pIndex=0; int.TryParse(this.hidToPage.Value,out pIndex); GoPage(pIndex); } } }
下面是调用方法:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="EB.Mall.Services.UI.Web.WebForm2" %> <%@ Register Src="~/Pageing1.ascx" TagPrefix="uc1" TagName="Pageing1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="查询" OnClick="Button1_Click" /> <uc1:Pageing1 runat="server" id="Pageing1" OnOnPageing="Pageing1_OnPageing" LoadShow="true" /> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace EB.Mall.Services.UI.Web { public partial class WebForm2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Pageing1_OnPageing(object obj, Pageing1.PageingEventArgs e, out int pageCount) { int pageIndex = e.PageIndex; //分解页面获取数据 pageCount = 20; } protected void Button1_Click(object sender, EventArgs e) { //不用获取数据,只写这一句就行 this.Pageing1.GoFirstPage(); } } }