2 // System.Web.UI.WebControls.DataBoundControl
5 // Ben Maurer (bmaurer@users.sourceforge.net)
11 using System.Collections;
12 using System.Collections.Specialized;
14 using System.Web.Util;
15 using System.ComponentModel;
17 namespace System.Web.UI.WebControls {
18 public abstract class DataBoundControl : WebControl
20 public event EventHandler DataBound;
22 protected DataBoundControl ()
26 public sealed override void DataBind ()
28 PerformDataBinding ();
29 RequiresDataBinding = false;
30 OnDataBound (EventArgs.Empty);
33 protected void EnsureDataBound ()
35 if (RequiresDataBinding && DataSourceID != "")
39 protected virtual object GetDataSourceObject()
41 if (DataSourceID != "")
42 return (IDataSource) NamingContainer.FindControl (DataSourceID);
48 protected virtual IEnumerable GetResolvedDataSource ()
50 if (DataSource != null && DataSourceID != "")
51 throw new HttpException ();
53 IDataSource ds = GetDataSourceObject () as IDataSource;
54 if (ds != null && DataSourceID != "")
55 return ds.GetView (DataMember).Select ();
56 else if (DataSource != null)
57 return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
62 protected bool IsBoundToDataSourceControl()
64 return (GetDataSourceObject () is IDataSource) && DataSourceID != "";
67 protected virtual void OnDataBound(EventArgs e) {
68 if (DataBound != null)
72 protected virtual void OnDataPropertyChanged ()
74 this.RequiresDataBinding = true;
77 protected virtual void OnDataSourceChanged (object sender, EventArgs e)
79 RequiresDataBinding = true;
82 // should be `internal protected' (why, oh WHY did they do that !?!)
83 protected override void OnInit (EventArgs e)
88 RequiresDataBinding = true;
91 // should be `internal protected' (why, oh WHY did they do that !?!)
92 protected override void OnLoad (EventArgs e)
94 IDataSource ds = GetDataSourceObject () as IDataSource;
95 if (ds != null && DataSourceID != "")
96 ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
101 // should be `internal protected' (why, oh WHY did they do that !?!)
102 protected override void OnPreRender (EventArgs e)
105 base.OnPreRender (e);
108 protected virtual void PerformDataBinding ()
110 OnDataBinding(EventArgs.Empty);
114 protected virtual void ValidateDataSource (object dataSource)
116 if (dataSource is IListSource || dataSource is IEnumerable)
118 throw new ArgumentException ();
122 public string DataMember
125 object o = ViewState["DataMember"];
131 ViewState["DataMember"] = value;
136 public virtual object DataSource
142 ValidateDataSource (value);
147 public virtual string DataSourceID {
149 object o = ViewState ["DataSourceID"];
157 RequiresDataBinding = true;
159 ViewState ["DataSourceID"] = value;
163 bool requiresDataBinding;
164 protected bool RequiresDataBinding {
165 get { return requiresDataBinding; }
166 set { requiresDataBinding = value; }
169 protected bool inited;