www.pudn.com > WebPager.rar > DataPagerBase.cs, change:2005-01-24,size:7599b


using System; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Hooyee.Utils; 
 
namespace Hooyee.WebControls { 
 
	#region MissingDataPagerContainerException 
	/// <summary> 
	/// 未指定数据翻页控件的容器类时所触发的错误。 
	/// </summary> 
	/// <version> 
	/// 20050120	Kriss 
	/// </version> 
	public sealed class MissingDataPagerContainerException : Exception { 
		/// <summary> 
		/// 初始化新的 MissingDataPagerContainerException 实例。 
		/// </summary> 
		public MissingDataPagerContainerException() : base("尚未指定数据翻页控件的容器类!") { 
		} 
	} 
	#endregion 
 
	#region Class DataPagerBase 
	/// <summary> 
	/// 用于实现数据翻页功能的用户控件的公共抽象基类。 
	/// </summary> 
	/// <remarks> 
	/// 请使用 <see cref="AcceptContainer"/> 方法为该控件指定容器。然后在容器中调用 <see cref="Render"/> 方法来呈现内容。也可以使用默认的容器(<see cref="DataPagerContainer"/>)来独立呈现该翻页控件。<BR/> 
	/// 翻页控件可以通过访问 <see cref="Container"/> 属性访问其容器。也可以通过 <see cref="PagerInfo">Container.PagerInfo</see> 访问容器的分页信息。 
	/// </remarks> 
	/// <exception cref="MissingDataPagerContainerException">未指定数据翻页控件的容器类时所触发的错误。</exception> 
	/// <example> 
	/// 为了实现 Url 翻页方式,容器类必须在其 OnLoad 事件写入以下代码。 
	/// <code> 
	/// protected override void OnLoad( EventArgs e ) { 
	///		if ( PageApplyType == PageApplyType.ByUrl ) { 
	///			PagerInfo.WebSupport.ApplyPageUrl( Page ); 
	///			ChangePageIndex( PagerInfo.WebSupport.UrlPageIndex ); 
	///		} 
	///		base.OnLoad( e ); 
	///	} 
	///	</code> 
	///	容器类在改变页面索引时,必须同时改变分页信息的索引。 
	///	<code> 
	///	public void ChangePageIndex( int newIndex ) { 
	///		pagerInfo.PageIndex = newIndex; 
	///	} 
	/// </code> 
	/// </example> 
	/// <version> 
	/// 20040827	Kriss 
	/// 20050119	Kriss	Update Interface and Content 
	/// </version> 
	public abstract class DataPagerBase : WebControl , IDataPager , INamingContainer { 
 
		#region 构造函数 
		/// <summary> 
		/// 默认构造函数 
		/// </summary> 
		protected DataPagerBase() { 
			this.ID = "DataPager"; 
		} 
		#endregion 
 
		#region GotoPage & Control 
		private void GotoPage( int newIndex ) { 
			PagerInfo.PageIndex = newIndex; 
			container.ChangePageIndex( newIndex ); 
		} 
 
		/// <summary> 
		/// 在跳转页面时所触发的事件 
		/// </summary> 
		/// <param name="sender"></param> 
		/// <param name="e"></param> 
		protected void OnPageLinkClick( object sender , EventArgs e ) { 
			int newIndex = 0; 
			if ( sender is LinkButton ) { 
				newIndex = int.Parse( ( sender as LinkButton ).CommandArgument ); 
			} else if ( sender is DropDownList ) { 
				newIndex = ( sender as DropDownList ).SelectedIndex; 
			} 
 
			GotoPage( newIndex ); 
		} 
 
		/// <summary> 
		/// 创建跳转页面按钮 
		/// </summary> 
		/// <param name="caption"></param> 
		/// <param name="newIndex"></param> 
		/// <returns></returns> 
		protected Control CreateGotoPageLink( string caption , int newIndex , string ID ) { 
			if ( pageApplyType == PageApplyType.PostBack ) { 
				LinkButton button = new LinkButton(); 
				button.ID = ID; 
				button.Text = caption; 
				button.CommandArgument = newIndex.ToString(); 
				button.Click += new EventHandler( this.OnPageLinkClick ); 
				return button; 
			} else if ( pageApplyType == PageApplyType.ByUrl ) { 
				string url = PagerInfo.WebSupport.GetPagingUrl( newIndex ); 
				Literal link = new Literal(); 
				link.Text = string.Format( "<a href='{0}'>{1}</a>" , url , caption ); 
				return link; 
			} 
			return null; 
		} 
		#endregion 
 
		#region Misc 
		/// <summary> 
		/// 获得指定数量的空格 
		/// </summary> 
		/// <param name="num"></param> 
		/// <returns></returns> 
		protected Literal GetSpace( int num ) { 
			Literal space = new Literal(); 
			for ( int i = 0 ; i < num ; i++ ) { 
				space.Text += " "; 
			} 
			return space; 
		} 
		#endregion 
 
		#region Render 
		/// <summary> 
		/// 呈现控件内容。 
		/// </summary> 
		public Control Render() { 
			try { 
				Table table = new Table(); 
				TableRow row = new TableRow(); 
				TableCell cell = new TableCell(); 
				row.Cells.Add( cell ); 
				table.Rows.Add( row ); 
 
				Render( cell ); 
				return table; 
			} catch { 
				Literal msg = new Literal(); 
				msg.Text = string.Format( "无法创建翻页控件 {0}。" , this.GetType().Name ); 
				return msg; 
			} 
		} 
 
		/// <summary> 
		/// 呈现控件内容。 
		/// </summary> 
		/// <param name="control">包含翻页控件的控件对象。</param> 
		public abstract void Render( Control control ); 
		#endregion 
 
		#region Container 
		/// <summary> 
		/// 是否含有容器。 
		/// </summary> 
		protected bool HasContainer { 
			get { 
				return ( null != container ); 
			} 
		} 
 
		private IDataPagerContainer container; 
		/// <summary> 
		/// 翻页控件容器。 
		/// </summary> 
		/// <exception cref="MissingDataPagerContainerException">未指定数据翻页控件的容器类时所触发的错误。</exception> 
		[System.ComponentModel.BrowsableAttribute( false )] 
		public IDataPagerContainer Container { 
			get { 
				if ( null == container ) 
					throw new MissingDataPagerContainerException(); 
				return container; 
			} 
		} 
 
		/// <summary> 
		/// 接受翻页控件容器。 
		/// </summary> 
		/// <remarks> 
		/// 指定翻页控件的容器对象。如果要将翻页控件呈现到容器中,请调用 <see cref="Render"/> 方法。 
		/// </remarks> 
		/// <param name="container">翻页控件容器</param> 
		public virtual void AcceptContainer( IDataPagerContainer container ) { 
			this.container = container; 
			this.pageApplyType = container.PageApplyType; 
			this.baseDir = container.BaseDir; 
			if ( !baseDir.EndsWith( "/" ) ) { 
				baseDir += "/"; 
			} 
		} 
 
		#region PagerInfo 
		/// <summary> 
		/// 获得数据翻页对象。 
		/// </summary> 
		[System.ComponentModel.BrowsableAttribute( false )] 
		public PagerInfo PagerInfo { 
			get { 
				return container.PagerInfo; 
			} 
		} 
		#endregion 
 
		#region BaseDir 
		private string baseDir; 
		/// <summary> 
		/// 获得基本路径。 
		/// </summary> 
		/// <remarks> 
		/// 基本路径是指该控件所用到的各种资源的基本相对路径。 
		/// </remarks> 
		[System.ComponentModel.BrowsableAttribute( false )] 
		protected string BaseDir { 
			get { 
				return baseDir; 
			} 
		} 
		#endregion 
		#endregion 
 
		#region PageApplyType 
		private PageApplyType pageApplyType = PageApplyType.ByUrl; 
		/// <summary> 
		/// 获得或设置使用哪种方式进行 Web 翻页。 
		/// </summary> 
		[System.ComponentModel.BrowsableAttribute( false )] 
		public PageApplyType PageApplyType { 
			get { 
				return pageApplyType; 
			} 
			set { 
				pageApplyType = value; 
			} 
		} 
		#endregion 
	} 
	#endregion 
 
	#region Class DataPagerFactory 
	/// <summary> 
	/// 用于创建翻页控件的工厂类。 
	/// </summary> 
	/// <version> 
	/// 20050121	Kriss 
	/// </version> 
	internal sealed class DataPagerFactory { 
		#region Instance 
		private DataPagerFactory() { 
		} 
 
		private IDataPager CreateDataPagerAgent( string pagerName ) { 
			try { 
				string pagerClassName = string.Concat( this.GetType().Namespace , "." , pagerName ); 
				IDataPager pager = this.GetType().Assembly.CreateInstance( pagerClassName ) as IDataPager; 
				return pager; 
			} catch { 
				return null; 
			} 
		} 
		#endregion 
 
		/// <summary> 
		/// 根据翻页控件名称创建翻页控件的实例。 
		/// </summary> 
		/// <param name="pagerName">翻页控件名称</param> 
		/// <returns>翻页控件的实例</returns> 
		public static IDataPager CreateDataPager( string pagerName ) { 
			return ( new DataPagerFactory() ).CreateDataPagerAgent( pagerName ); 
		} 
	} 
	#endregion 
}