**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Web / System.Web.UI.WebControls / DataBoundControl.cs
blob9a140b27e758786ba1e8295d534904da19039de0
1 //
2 // System.Web.UI.WebControls.DataBoundControl
3 //
4 // Authors:
5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 //
7 // (C) 2003 Ben Maurer
8 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #if NET_2_0
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Text;
35 using System.Web.Util;
36 using System.ComponentModel;
38 namespace System.Web.UI.WebControls {
39 public abstract class DataBoundControl : WebControl
41 public event EventHandler DataBound;
43 protected DataBoundControl ()
47 public sealed override void DataBind ()
49 PerformDataBinding ();
50 RequiresDataBinding = false;
51 OnDataBound (EventArgs.Empty);
54 protected void EnsureDataBound ()
56 if (RequiresDataBinding && DataSourceID != "")
57 DataBind ();
60 protected virtual object GetDataSourceObject()
62 if (DataSourceID != "")
63 return (IDataSource) NamingContainer.FindControl (DataSourceID);
65 return DataSource;
69 protected virtual IEnumerable GetResolvedDataSource ()
71 if (DataSource != null && DataSourceID != "")
72 throw new HttpException ();
74 IDataSource ds = GetDataSourceObject () as IDataSource;
75 if (ds != null && DataSourceID != "")
76 return ds.GetView (DataMember).Select ();
77 else if (DataSource != null)
78 return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
79 else
80 return null;
83 protected bool IsBoundToDataSourceControl()
85 return (GetDataSourceObject () is IDataSource) && DataSourceID != "";
88 protected virtual void OnDataBound(EventArgs e) {
89 if (DataBound != null)
90 DataBound (this, e);
93 protected virtual void OnDataPropertyChanged ()
95 this.RequiresDataBinding = true;
98 protected virtual void OnDataSourceChanged (object sender, EventArgs e)
100 RequiresDataBinding = true;
103 // should be `internal protected' (why, oh WHY did they do that !?!)
104 protected override void OnInit (EventArgs e)
106 base.OnInit(e);
107 inited = true;
108 if (!Page.IsPostBack)
109 RequiresDataBinding = true;
112 // should be `internal protected' (why, oh WHY did they do that !?!)
113 protected override void OnLoad (EventArgs e)
115 IDataSource ds = GetDataSourceObject () as IDataSource;
116 if (ds != null && DataSourceID != "")
117 ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
119 base.OnLoad(e);
122 // should be `internal protected' (why, oh WHY did they do that !?!)
123 protected override void OnPreRender (EventArgs e)
125 EnsureDataBound ();
126 base.OnPreRender (e);
129 protected virtual void PerformDataBinding ()
131 OnDataBinding(EventArgs.Empty);
135 protected virtual void ValidateDataSource (object dataSource)
137 if (dataSource is IListSource || dataSource is IEnumerable)
138 return;
139 throw new ArgumentException ();
143 public string DataMember
145 get {
146 object o = ViewState["DataMember"];
147 if(o!=null)
148 return (string)o;
149 return String.Empty;
151 set {
152 ViewState["DataMember"] = value;
156 object dataSource;
157 public virtual object DataSource
159 get {
160 return dataSource;
162 set {
163 ValidateDataSource (value);
164 dataSource = value;
168 public virtual string DataSourceID {
169 get {
170 object o = ViewState ["DataSourceID"];
171 if (o != null)
172 return (string)o;
174 return String.Empty;
176 set {
177 if (inited)
178 RequiresDataBinding = true;
180 ViewState ["DataSourceID"] = value;
184 bool requiresDataBinding;
185 protected bool RequiresDataBinding {
186 get { return requiresDataBinding; }
187 set { requiresDataBinding = value; }
190 protected bool inited;
193 #endif