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


using System; 
using System.Collections; 
using System.Collections.Specialized; 
using System.Reflection; 
using System.ComponentModel; 
using System.Drawing.Design; 
using System.Web; 
using System.Web.UI.WebControls; 
using Hooyee.Utils; 
 
namespace Hooyee.WebControls { 
	/// <summary> 
	/// 高级数据表格控件。 
	/// </summary> 
	/// <remarks> 
	/// 支持自定义翻页子控件。支持定制 CSS 。支持单行和双行的动态交错效果。 
	/// </remarks> 
	/// <example> 
	/// <code> 
	/// private void Page_Load() { 
	///		AdvDataGrid grid = new AdvDataGrid(); 
	///		grid.AfterPageChanged += new DataGridPageChangedEventHandler(OnPageChanged); 
	/// } 
	///  
	/// private void OnPageChanged(object sender, DataGridPageChangedEventArgs e) { 
	///		AdvDataGrid grid = sender as AdvDataGrid; 
	///		string wkSql = "select * from Customers"; 
	///		grid.DataSource = SqlScope.FillDataTable( wkSql, "Customers", grid.PagerInfo.PageIndex, grid.PageSize ); 
	///		grid.DataBind(); 
	/// } 
	/// </code> 
	/// </example> 
	public sealed class AdvDataGrid : DataGrid , IDataPagerContainer { 
		#region 构造函数 
		/// <summary> 
		/// 初始化 AdvDataGrid 类的新实例。 
		/// </summary> 
		public AdvDataGrid() { 
			this.ItemCreated += new DataGridItemEventHandler( AdvDataGrid_ItemCreated ); 
		} 
		#endregion 
 
		#region Override Properties 
		/// <summary> 
		/// 要在 <see cref="DataGrid"/> 控件的单页上显示的项数。默认值为 10。 
		/// </summary> 
		public override int PageSize { 
			get { 
				return base.PageSize; 
			} 
			set { 
				base.PageSize = value; 
				PagerInfo.PageSize = value; 
			} 
		} 
 
		/// <summary> 
		/// 在使用自定义分页时 <see cref="System.Web.UI.WebControls.DataGrid"/> 控件中的实际项数。 
		/// </summary> 
		public override int VirtualItemCount { 
			get { 
				return base.VirtualItemCount; 
			} 
			set { 
				base.VirtualItemCount = value; 
				PagerInfo.ItemCount = value; 
			} 
		} 
		#endregion 
 
		#region 属性、事件 
		private string imageBase = "images/"; 
		/// <summary> 
		/// 获得或设置基本路径。 
		/// </summary> 
		/// <remarks> 
		/// 基本路径是指该控件所用到的各种资源的基本相对路径。 
		/// </remarks> 
		[Category( "高级属性" ) , Description( "获得或设置基本路径。" ) , BrowsableAttribute( true )] 
		public string BaseDir { 
			get { 
				return imageBase; 
			} 
			set { 
				imageBase = value; 
			} 
		} 
 
		private string styleClass = string.Empty; 
		/// <summary> 
		/// CSS样式组 
		/// </summary> 
		[Category( "高级属性" ) , Description( "CSS样式组" ) , BrowsableAttribute( true )] 
		public string StyleGroup { 
			get { 
				return styleClass; 
			} 
			set { 
				styleClass = value; 
			} 
		} 
 
		private PageApplyType pageApplyType = PageApplyType.ByUrl; 
		/// <summary> 
		/// 获得或设置使用哪种方式进行 Web 翻页。 
		/// </summary> 
		[Category( "高级属性" ) , Description( "获得或设置使用哪种方式进行 Web 翻页。" ) 
		, BrowsableAttribute( true ) , DefaultValue( PageApplyType.ByUrl )] 
		public PageApplyType PageApplyType { 
			get { 
				return pageApplyType; 
			} 
			set { 
				pageApplyType = value; 
			} 
		} 
 
		private string pagerClass = "PreNextListJumpPager"; 
		/// <summary> 
		/// 获取或设置翻页控件名称。 
		/// </summary> 
		[Category( "高级属性" ) , Description( "获取或设置翻页控件名称。" ) , BrowsableAttribute( true ) , 
		DefaultValue( "PreNextListJumpPager" ) , Editor( typeof( DataPagerEditor ) , typeof( System.Drawing.Design.UITypeEditor ) )] 
		public string PagerClass { 
			get { 
				return pagerClass; 
			} 
			set { 
				if ( pagerClass == value ) 
					return; 
				pagerClass = value; 
				dataPager = null; 
			} 
		} 
 
		private bool styleEnabled = true; 
		/// <summary> 
		/// 是否使用 CSS 式样 
		/// </summary> 
		[Category( "高级属性" ) , Description( "是否使用 CSS 式样" ) , BrowsableAttribute( true )] 
		public bool StyleEnabled { 
			get { 
				return styleEnabled; 
			} 
			set { 
				styleEnabled = value; 
			} 
		} 
 
		private bool selectionEffect = false; 
		/// <summary> 
		/// 是否使用选择列动态效果 
		/// </summary> 
		[Category( "高级属性" ) , Description( "是否使用选择列动态效果" ) , BrowsableAttribute( true )] 
		public bool SelectionEffect { 
			get { 
				return selectionEffect; 
			} 
			set { 
				selectionEffect = value; 
			} 
		} 
 
		/// <summary> 
		/// 当进行翻页时触发,一般进行数据绑定。 
		/// </summary> 
		/// <remarks> 
		/// 该事件一般发生在页面索引被修改以后,留给容器外界来处理翻页。<BR/> 
		/// <note>请不要在该事件处理方法中再次调用 <B>ChangePageIndex</B> 方法。这将可能造成死循环。</note> 
		/// </remarks> 
		[Category( "分页" ) , Description( "当进行翻页时触发,一般进行数据绑定。" ) , BrowsableAttribute( true )] 
		public event DataGridPageChangedEventHandler AfterPageChanged; 
		#endregion 
 
		#region PagerInfo 
 
		private IDataPager dataPager; 
		/// <summary> 
		/// 获得数据翻页对象。 
		/// </summary> 
		[Category( "高级属性" ) , Description( "获得数据翻页对象。" ) , BrowsableAttribute( false ) , DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )] 
		public IDataPager DataPager { 
			get { 
				if ( null == dataPager ) 
					dataPager = CreateDataPager(); 
				return dataPager; 
			} 
		} 
 
		private PagerInfo pagerInfo; 
		/// <summary> 
		/// 获得或设置数据翻页信息。 
		/// </summary> 
		[Category( "高级属性" ) , Description( "获得或设置数据翻页信息。" ) , BrowsableAttribute( false ) , DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )] 
		public PagerInfo PagerInfo { 
			get { 
				if ( null == pagerInfo ) 
					pagerInfo = new PagerInfo( this.ViewState ); 
				return pagerInfo; 
			} 
			set { 
				pagerInfo = value; 
			} 
		} 
		#endregion 
 
		#region Event 
		/// <summary> 
		/// 改变页面索引。 
		/// </summary> 
		/// <param name="newIndex">新的页面索引</param> 
		/// <event cref="AfterPageChanged">当进行翻页时触发,进行绑顶数据</event> 
		public void ChangePageIndex( int newIndex ) { 
			pagerInfo.PageIndex = newIndex; 
			if ( this.AfterPageChanged != null ) { 
				DataGridPageChangedEventArgs args = new DataGridPageChangedEventArgs( this , newIndex ); 
				AfterPageChanged( this , args ); 
			} 
		} 
 
		/// <summary> 
		/// 重写 <see cref = "System.Web.UI.Control.OnLoad"/> 方法。 
		/// </summary> 
		/// <param name = "e">包含事件数据的 <see cref = "EventArgs"/> 对象。</param> 
		protected override void OnLoad( EventArgs e ) { 
			// CSS 样式 
			if ( styleEnabled ) { 
				this.CssClass = styleClass + "DG_Table"; 
				this.HeaderStyle.CssClass = styleClass + "DG_Header"; 
				this.FooterStyle.CssClass = styleClass + "DG_Footer"; 
				this.AlternatingItemStyle.CssClass = styleClass + "DG_Alter"; 
				this.ItemStyle.CssClass = styleClass + "DG_Item"; 
				this.PagerStyle.CssClass = styleClass + "DG_Paper"; 
			} 
			if ( pageApplyType == PageApplyType.ByUrl ) { 
				PagerInfo.WebSupport.ApplyPageUrl( Page ); 
				ChangePageIndex( PagerInfo.WebSupport.UrlPageIndex ); 
			} 
			base.OnLoad( e ); 
		} 
 
		/// <summary> 
		/// 创建翻页控件 
		/// </summary> 
		/// <returns>翻页控件</returns> 
		private IDataPager CreateDataPager() { 
			IDataPager pager = DataPagerFactory.CreateDataPager( pagerClass ); 
			if ( null != pager ) 
				pager.AcceptContainer( this ); 
			return pager; 
		} 
 
		private void AdvDataGrid_ItemCreated( object sender , System.Web.UI.WebControls.DataGridItemEventArgs e ) { 
 
			// 选择列动态效果 
			if ( selectionEffect ) { 
				if ( e.Item.ItemType == ListItemType.Item ) { 
					e.Item.Attributes.Add( "OnMouseOver" , "JavaScript:this.className='" + styleClass + "DG_Over'" ); 
					e.Item.Attributes.Add( "OnMouseOut" , "JavaScript:this.className='" + styleClass + "DG_Item'" ); 
					return; 
				} else if ( e.Item.ItemType == ListItemType.AlternatingItem ) { 
					e.Item.Attributes.Add( "OnMouseOver" , "JavaScript:this.className='" + styleClass + "DG_Over'" ); 
					e.Item.Attributes.Add( "OnMouseOut" , "JavaScript:this.className='" + styleClass + "DG_Alter'" ); 
					return; 
				} 
			} 
 
			// 翻页控件 
			if ( e.Item.ItemType == ListItemType.Pager ) { 
				try { 
					DataPager.Render( ( TableCell ) e.Item.Controls[0] ); 
				} catch ( Exception ex ) { 
					Literal msg = new Literal(); 
					msg.Text = string.Format( "创建错误:{0}" , ex.Message ); 
					( ( TableCell ) e.Item.Controls[0] ).Controls.Add( msg ); 
				} 
			} 
		} 
		#endregion 
	} 
}