2 // System.Web.UI.WebControls.DataBoundControl
5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 // Sanjay Gupta (gsanjay@novell.com)
9 // (C) 2004-2010 Novell, Inc. (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System
.Collections
;
34 using System
.Collections
.Specialized
;
36 using System
.Web
.Util
;
37 using System
.ComponentModel
;
38 using System
.Security
.Permissions
;
39 using System
.Web
.UI
.WebControls
.Adapters
;
41 namespace System
.Web
.UI
.WebControls
44 [AspNetHostingPermission (SecurityAction
.LinkDemand
, Level
= AspNetHostingPermissionLevel
.Minimal
)]
45 [AspNetHostingPermission (SecurityAction
.InheritanceDemand
, Level
= AspNetHostingPermissionLevel
.Minimal
)]
47 [DesignerAttribute ("System.Web.UI.Design.WebControls.DataBoundControlDesigner, " + Consts
.AssemblySystem_Design
, "System.ComponentModel.Design.IDesigner")]
48 public abstract class DataBoundControl
: BaseDataBoundControl
50 DataSourceSelectArguments selectArguments
;
51 DataSourceView currentView
;
53 protected DataBoundControl ()
57 /* Used for controls that used to inherit from
58 * WebControl, so the tag can propagate upwards
60 internal DataBoundControl (HtmlTextWriterTag tag
) : base (tag
)
64 protected virtual IDataSource
GetDataSource ()
66 if (IsBoundUsingDataSourceID
) {
67 Control ctrl
= FindDataSource ();
70 throw new HttpException (string.Format ("A control with ID '{0}' could not be found.", DataSourceID
));
71 if (!(ctrl
is IDataSource
))
72 throw new HttpException (string.Format ("The control with ID '{0}' is not a control of type IDataSource.", DataSourceID
));
73 return (IDataSource
) ctrl
;
76 IDataSource ds
= DataSource
as IDataSource
;
80 IEnumerable ie
= DataSourceResolver
.ResolveDataSource (DataSource
, DataMember
);
81 return new CollectionDataSource (ie
);
84 protected virtual DataSourceView
GetData ()
86 if (currentView
== null)
91 DataSourceView
InternalGetData ()
93 if (currentView
!= null)
96 if (DataSource
!= null && IsBoundUsingDataSourceID
)
97 throw new HttpException ("Control bound using both DataSourceID and DataSource properties.");
99 IDataSource ds
= GetDataSource ();
101 return ds
.GetView (DataMember
);
106 protected override void OnDataPropertyChanged ()
108 base.OnDataPropertyChanged ();
112 protected virtual void OnDataSourceViewChanged (object sender
, EventArgs e
)
114 RequiresDataBinding
= true;
117 // MSDN: The OnPagePreLoad method is overridden by the DataBoundControl class
118 // to set the BaseDataBoundControl.RequiresDataBinding property to true in
119 // cases where the HTTP request is a postback and view state is enabled but
120 // the data-bound control has not yet been bound.
122 // LAMESPEC: RequiresDataBinding is also set when http request is NOT a postback -
123 // no matter whether view state is enabled. The correct description should be:
125 // The OnPagePreLoad method is override by the DataBoundControl class
126 // to set the BaseDataBoundControl.RequiresDataBinding property to true in
127 // cases where the HTTP request is not a postback or it is a postback and view state
128 // is enabled but the data-bound control has not yet been bound.
129 protected override void OnPagePreLoad (object sender
, EventArgs e
)
131 base.OnPagePreLoad (sender
, e
);
139 if (page
!= null && !IsDataBound
) {
140 // LAMESPEC: see the comment above OnPagePreLoad
141 if (!page
.IsPostBack
)
142 RequiresDataBinding
= true;
143 else if (IsViewStateEnabled
)
144 RequiresDataBinding
= true;
149 void UpdateViewData ()
151 if (currentView
!= null)
152 currentView
.DataSourceViewChanged
-= new EventHandler (OnDataSourceViewChanged
);
154 DataSourceView view
= InternalGetData ();
155 if (view
!= currentView
)
158 if (currentView
!= null)
159 currentView
.DataSourceViewChanged
+= new EventHandler (OnDataSourceViewChanged
);
162 protected internal override void OnLoad (EventArgs e
)
168 // MSDN: The ConfirmInitState method sets the initialized state of the data-bound
169 // control. The method is called by the DataBoundControl class in its OnLoad
177 protected internal virtual void PerformDataBinding (IEnumerable data
)
181 protected override void ValidateDataSource (object dataSource
)
183 if (dataSource
== null || dataSource
is IListSource
|| dataSource
is IEnumerable
|| dataSource
is IDataSource
)
185 throw new ArgumentException ("Invalid data source source type. The data source must be of type IListSource, IEnumerable or IDataSource.");
188 [ThemeableAttribute (false)]
189 [DefaultValueAttribute ("")]
190 [WebCategoryAttribute ("Data")]
191 public virtual string DataMember
{
192 get { return ViewState.GetString ("DataMember", String.Empty); }
193 set { ViewState["DataMember"] = value; }
196 [IDReferencePropertyAttribute (typeof(DataSourceControl
))]
197 public override string DataSourceID
{
198 get { return ViewState.GetString ("DataSourceID", String.Empty); }
200 ViewState
["DataSourceID"] = value;
201 base.DataSourceID
= value;
206 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
207 public IDataSource DataSourceObject
{
208 get { return GetDataSource (); }
212 // See DataBoundControl.MarkAsDataBound msdn doc for the code example
214 protected override void PerformSelect ()
216 // Call OnDataBinding here if bound to a data source using the
217 // DataSource property (instead of a DataSourceID), because the
218 // databinding statement is evaluated before the call to GetData.
219 if (!IsBoundUsingDataSourceID
)
220 OnDataBinding (EventArgs
.Empty
);
222 // prevent recursive calls
223 RequiresDataBinding
= false;
224 SelectArguments
= CreateDataSourceSelectArguments ();
225 GetData ().Select (SelectArguments
, new DataSourceViewSelectCallback (OnSelect
));
227 // The PerformDataBinding method has completed.
230 // Raise the DataBound event.
231 OnDataBound (EventArgs
.Empty
);
234 void OnSelect (IEnumerable data
)
236 // Call OnDataBinding only if it has not already been
237 // called in the PerformSelect method.
238 if (IsBoundUsingDataSourceID
)
239 OnDataBinding (EventArgs
.Empty
);
240 // The PerformDataBinding method binds the data in the
241 // retrievedData collection to elements of the data-bound control.
242 InternalPerformDataBinding (data
);
245 internal void InternalPerformDataBinding (IEnumerable data
)
247 DataBoundControlAdapter adapter
= Adapter
as DataBoundControlAdapter
;
249 adapter
.PerformDataBinding (data
);
251 PerformDataBinding (data
);
254 protected virtual DataSourceSelectArguments
CreateDataSourceSelectArguments ()
256 return DataSourceSelectArguments
.Empty
;
259 protected DataSourceSelectArguments SelectArguments
{
261 if (selectArguments
== null)
262 selectArguments
= CreateDataSourceSelectArguments ();
263 return selectArguments
;
266 selectArguments
= value;
272 object dataBound
= ViewState
["DataBound"];
273 return dataBound
!= null ? (bool) dataBound
: false;
276 ViewState
["DataBound"] = value;
280 protected void MarkAsDataBound ()