日期:2014-05-20 浏览次数:20562 次
using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text.RegularExpressions; using System.Collections; namespace YSMV.Control { [DefaultProperty("Text")] [ToolboxData("<{0}:YsmvRepeater runat=\"server\" AllowPage=\"true\" PageSize=\"10\" ShowPage=\"true\"></{0}:YsmvRepeater>")] public class YsmvRepeater : Repeater { //Static param private static readonly Regex RX; private static readonly object EventPageChanged; static YsmvRepeater() { RX = new Regex(@"^&page=\d+", RegexOptions.Compiled); EventPageChanged = new object(); } //Static constants protected const string HTML1 = "<table cellpadding=0 cellspacing=0 class='inputTable'><tr><td colspan=2>"; protected const string HTML2 = "</td></tr><tr><td class=paging align=left>"; protected const string HTML3 = "</td><td align=right class=paging>"; protected const string HTML4 = "</td></tr></table>"; private const string LINK_PREV = "<a href=?page={0}>< Previous</a>"; private const string LINK_MORE = "<a href=?page={0}>More ></a>"; private const string KEY_PAGE = "page"; private const string COMMA = "?"; private const string AMP = "&"; protected string emptyText; private IList dataSource; private int pageSize = 10; private int currentPageIndex; private int itemCount; private bool allowPage = false; private bool showPage = true; [Browsable(true) , Category("Appearance"), DefaultValue(true), Description("是否显示翻页链接") ] public bool ShowPage { get { return showPage; } set { showPage = value; } } [Browsable(true), Category("Appearance"), DefaultValue(true), Description("是否允许分页")] public bool AllowPage { get { return allowPage; } set { allowPage = value; } } [Browsable(true), Category("Appearance"), DefaultValue(10), Description("每页显示的数量") ] public int PageSize { get { return pageSize; } set { pageSize = value; } } [Browsable(true), Category("Appearance"), DefaultValue(""), Description("无记录时候显示的内容")] public string EmptyText { set { emptyText = value; } } override public object DataSource { set { //This try catch block is to avoid issues with the VS.NET designer //The designer will try and bind a datasource which does not derive from ILIST try { dataSource = (IList)value; ItemCount = dataSource.Count; } catch { dataSource = null; ItemCount = 0; } } } protected int PageCount { get { return (ItemCount - 1) / pageSize; } } virtual public int ItemCount { get { return itemCount; } set {itemCount = value;} } virtual public int CurrentPageIndex { get { return currentPageIndex; } set { currentPageIndex = value; } } public void SetPage(int index) { //OnPageIndexChanged(new DataGridPageChangedEventArgs(null, index)); OnPageIndexChanged(new GridViewPageEventArgs(index)); } override protected void OnLoad(EventArgs e) { if (Visible) { string page = Context.Request[KEY_PAGE]; int index = (page != null) ? int.Parse(page) : 0; SetPage(index); } } /// <summary> /// Overriden method to control how the page is rendered /// </summary> /// <param name="writer"></param> override protected void Render(HtmlTextWriter writer) { //Check there is some data attached string page = Context.Request[KEY_PAGE]; int index = (page != null) ? int.Parse(page) : 0; if (ItemCount == 0) { writer.Write(emptyText); return; } //Mask the query string query = Context.Request.Url.Query.Replace(COMMA, AMP); query = RX.Replace(query, string.Empty); // Write out the first part of the control, the table header writer.Write(HTML1); // Call the inherited method base.Render(writer); // Write out a table row closure writer.Write(HTML2); //Determin whether next and previous buttons are required //Previous button? if (currentPageIndex > 0) { if (ShowPage) writer.Write(string.Format(LINK_PREV, (currentPageIndex - 1) + query)); } //Close the table data tag writer.Write(HTML3); //Next button? if (currentPageIndex < PageCount) { if (ShowPage) writer.Write(string.Format(LINK_MORE, (currentPageIndex + 1) + query)); } //Close the table writer.Write(HTML4); } override protected void OnDataBinding(EventArgs e) { if (!allowPage) { base.DataSource = dataSource; base.OnDataBinding(e); return; } //Work out which items we want to render to the page int start = CurrentPageIndex * pageSize; int size = Math.Min(pageSize, ItemCount - start); IList page = new ArrayList(); //Add the relevant items from the datasource for (int i = 0; i < size; i++) page.Add(dataSource[start + i]); //set the base objects datasource base.DataSource = page; base.OnDataBinding(e); } //public event DataGridPageChangedEventHandler PageIndexChanged; public event GridViewPageEventHandler PageIndexChanged { add { Events.AddHandler(EventPageChanged, value); } remove { Events.RemoveHandler(EventPageChanged, value); } } virtual protected void OnPageIndexChanged(GridViewPageEventArgs e) { GridViewPageEventHandler pagehandler = (GridViewPageEventHandler)Events[EventPageChanged]; if (pagehandler != null) pagehandler(this, e); } } }