浏览代码

2003-11-09 Ben Maurer <[email protected]>

	* DataBoundControl.cs: Implement
	* ListControl.cs: inherit from the above.
	* Repeater.cs, BaseDataList.cs: should reset whenever we databind.

svn path=/trunk/mcs/; revision=19773
Ben Maurer 22 年之前
父节点
当前提交
19d8d5d139

+ 4 - 1
mcs/class/System.Web/System.Web.UI.WebControls/BaseDataList.cs

@@ -51,7 +51,10 @@ namespace System.Web.UI.WebControls
 		}
 
 		public override void DataBind()
-		{
+		{
+			#if NET_1_2
+			RequiresDataBinding = false;
+			#endif
 			OnDataBinding(EventArgs.Empty);
 		}
 

+ 6 - 0
mcs/class/System.Web/System.Web.UI.WebControls/ChangeLog

@@ -1,3 +1,9 @@
+2003-11-09 Ben Maurer  <[email protected]>
+
+	* DataBoundControl.cs: Implement 
+	* ListControl.cs: inherit from the above.
+	* Repeater.cs, BaseDataList.cs: should reset whenever we databind.
+
 2003-11-09 Ben Maurer  <[email protected]>
 
 	* BaseDataList.cs:

+ 173 - 0
mcs/class/System.Web/System.Web.UI.WebControls/DataBoundControl.cs

@@ -0,0 +1,173 @@
+//
+// System.Web.UI.WebControls.DataBoundControl
+//
+// Authors:
+//	Ben Maurer ([email protected])
+//
+// (C) 2003 Ben Maurer
+//
+
+#if NET_1_2
+using System.Collections;
+using System.Collections.Specialized;
+using System.Text;
+using System.Web.Util;
+using System.ComponentModel;
+
+namespace System.Web.UI.WebControls {
+	public abstract class DataBoundControl : WebControl
+	{
+		public event EventHandler DataBound;
+		
+		protected DataBoundControl ()
+		{
+		}
+		
+		public sealed override void DataBind ()
+		{
+			PerformDataBinding ();
+			RequiresDataBinding = false;
+			OnDataBound (EventArgs.Empty);
+		}
+		
+		protected void EnsureDataBound ()
+		{
+			if (RequiresDataBinding && DataSourceID != "")
+				DataBind ();
+		}
+
+		protected virtual object GetDataSourceObject()
+		{
+			if (DataSourceID != "")
+				return (IDataSource) NamingContainer.FindControl (DataSourceID);
+			
+			return DataSource;
+		}
+		
+		
+		protected virtual IEnumerable GetResolvedDataSource ()
+		{
+			if (DataSource != null && DataSourceID != "")
+				throw new HttpException ();
+			
+			IDataSource ds = GetDataSourceObject () as IDataSource;
+			if (ds != null && DataSourceID != "")
+				return ds.GetView (DataMember).Select ();
+			else if (DataSource != null)
+				return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
+			else
+				return null; 
+		}
+		
+		protected bool IsBoundToDataSourceControl()
+		{
+			return (GetDataSourceObject () is IDataSource) && DataSourceID != "";
+		}
+		
+		protected virtual void OnDataBound(EventArgs e) {
+			if (DataBound != null)
+				DataBound (this, e);
+		}
+		
+		protected virtual void OnDataPropertyChanged ()
+		{
+			this.RequiresDataBinding = true;
+		}
+		
+		protected virtual void OnDataSourceChanged (object sender, EventArgs e)
+		{
+			RequiresDataBinding = true;
+		}
+		
+		// should be `internal protected' (why, oh WHY did they do that !?!)
+		protected override void OnInit (EventArgs e)
+		{
+			base.OnInit(e);
+			inited = true;
+			if (!Page.IsPostBack)
+				RequiresDataBinding = true;
+		}
+		
+		// should be `internal protected' (why, oh WHY did they do that !?!)
+		protected override void OnLoad (EventArgs e)
+		{
+			IDataSource ds = GetDataSourceObject () as IDataSource;
+			if (ds != null && DataSourceID != "")
+				ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
+			
+			base.OnLoad(e);
+		}
+		
+		// should be `internal protected' (why, oh WHY did they do that !?!)
+		protected override void OnPreRender (EventArgs e)
+		{
+			EnsureDataBound ();
+			base.OnPreRender (e);
+		}
+		
+		protected virtual void PerformDataBinding ()
+		{
+			OnDataBinding(EventArgs.Empty);
+		}
+
+		
+		protected virtual void ValidateDataSource (object dataSource)
+		{
+			if (dataSource is IListSource || dataSource is IEnumerable)
+				return;
+			throw new ArgumentException ();
+		}
+
+
+		public string DataMember
+		{
+			get {
+				object o = ViewState["DataMember"];
+				if(o!=null)
+					return (string)o;
+				return String.Empty;
+			}
+			set {
+				ViewState["DataMember"] = value;
+			}
+		}
+
+		object dataSource;
+		public virtual object DataSource
+		{
+			get {
+				return dataSource;
+			}
+			set {
+				ValidateDataSource (value);
+				dataSource = value;
+			}
+		}
+		
+		public virtual string DataSourceID {
+			get {
+				object o = ViewState ["DataSourceID"];
+				if (o != null)
+					return (string)o;
+				
+				return String.Empty;
+			}
+			set {
+				if (inited)
+					RequiresDataBinding = true;
+				
+				ViewState ["DataSourceID"] = value;
+			}
+		}
+		
+		bool requiresDataBinding;
+		protected bool RequiresDataBinding {
+			get { return requiresDataBinding; }
+			set { requiresDataBinding = value; }
+		}
+		
+		protected bool inited;
+	}
+}
+#endif
+

+ 34 - 10
mcs/class/System.Web/System.Web.UI.WebControls/ListControl.cs

@@ -20,23 +20,39 @@ using System.Web.Util;
 namespace System.Web.UI.WebControls
 {
 	[DefaultEvent("SelectedIndexChanged")]
-	[DefaultProperty("DataSource")]
+	#if !NET_1_2
+	[DefaultProperty("DataSource")]
+	#endif
 	[Designer ("System.Web.UI.Design.WebControls.ListControlDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
 	[DataBindingHandler("System.Web.UI.Design.ListControlDataBindingHandler, " + Consts.AssemblySystem_Design)]
 	[ParseChildren(true, "Items")]
-	public abstract class ListControl: WebControl
+	public abstract class ListControl : 
+		#if NET_1_2
+			DataBoundControl
+		#else
+			WebControl
+		#endif
 	{
 		private static readonly object SelectedIndexChangedEvent = new object();
-
-		private object             dataSource;
+
+		#if !NET_1_2
+		private object             dataSource;
+		#endif
+		
 		private ListItemCollection items;
 
 		private int cachedSelectedIndex = -1;
 		private string cachedSelectedValue;
-
+
+		#if !NET_1_2
 		public ListControl(): base(HtmlTextWriterTag.Select)
 		{
-		}
+		}
+		#else
+		protected override HtmlTextWriterTag TagKey {
+			get { return HtmlTextWriterTag.Select; }
+		}
+		#endif
 
 		[WebCategory ("Action")]
 		[WebSysDescription ("Raised when the selected index entry has changed.")]
@@ -69,6 +85,7 @@ namespace System.Web.UI.WebControls
 			}
 		}
 
+		#if !NET_1_2
 		[DefaultValue (""), WebCategory ("Data")]
 		[WebSysDescription ("The name of the table that is used for binding when a DataSource is specified.")]
 		public virtual string DataMember
@@ -104,7 +121,8 @@ namespace System.Web.UI.WebControls
 				}
 				throw new ArgumentException(HttpRuntime.FormatResourceString(ID, "Invalid DataSource Type"));
 			}
-		}
+		}
+		#endif
 
 		[DefaultValue (""), WebCategory ("Data")]
 		[WebSysDescription ("The field in the datatable that provides the text entry.")]
@@ -297,12 +315,18 @@ namespace System.Web.UI.WebControls
 				}
 			}
 		}
-
+
+		#if NET_1_2
+		protected override void PerformDataBinding ()
+		{
+			base.PerformDataBinding ();
+			IEnumerable ds = GetResolvedDataSource ();
+		#else
 		protected override void OnDataBinding(EventArgs e)
 		{
 			base.OnDataBinding(e);
-			IEnumerable ds = DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
-
+			IEnumerable ds = DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
+		#endif
 			if(ds != null) {
 				string dtf = DataTextField;
 				string dvf = DataValueField;

+ 3 - 0
mcs/class/System.Web/System.Web.UI.WebControls/Repeater.cs

@@ -234,6 +234,9 @@ namespace System.Web.UI.WebControls
 
 		public override void DataBind ()
 		{
+			#if NET_1_2
+			RequiresDataBinding = false;
+			#endif
 			OnDataBinding (EventArgs.Empty);
 		}
 

+ 1 - 0
mcs/class/System.Web/System.Web.dll.sources

@@ -463,3 +463,4 @@ System.Web.UI.WebControls/XmlHierarchicalDataSourceView.cs
 System.Web.UI.WebControls/XmlHierarchicalEnumerable.cs
 System.Web.UI.WebControls/XmlHierarchyData.cs
 System.Web.UI/HierarchicalDataSourceControl.cs
+System.Web.UI.WebControls/DataBoundControl.cs