2 // System.Web.UI.WebControls.DataBoundControl
5 // Ben Maurer (bmaurer@users.sourceforge.net)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System
.Collections
;
33 using System
.Collections
.Specialized
;
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
!= "")
60 protected virtual object GetDataSourceObject()
62 if (DataSourceID
!= "")
63 return (IDataSource
) NamingContainer
.FindControl (DataSourceID
);
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
);
83 protected bool IsBoundToDataSourceControl()
85 return (GetDataSourceObject () is IDataSource
) && DataSourceID
!= "";
88 protected virtual void OnDataBound(EventArgs e
) {
89 if (DataBound
!= null)
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
)
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
);
122 // should be `internal protected' (why, oh WHY did they do that !?!)
123 protected override void OnPreRender (EventArgs e
)
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
)
139 throw new ArgumentException ();
143 public string DataMember
146 object o
= ViewState
["DataMember"];
152 ViewState
["DataMember"] = value;
157 public virtual object DataSource
163 ValidateDataSource (value);
168 public virtual string DataSourceID
{
170 object o
= ViewState
["DataSourceID"];
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
;