2 // System.Web.UI.WebControls.DataBoundControl
5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 // Sanjay Gupta (gsanjay@novell.com)
9 // (C) 2004 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.
34 using System.Collections;
35 using System.Collections.Specialized;
37 using System.Web.Util;
38 using System.ComponentModel;
39 using System.Security.Permissions;
40 using System.Web.UI.WebControls.Adapters;
42 namespace System.Web.UI.WebControls {
45 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
46 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
48 [DesignerAttribute ("System.Web.UI.Design.WebControls.DataBoundControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
49 public abstract class DataBoundControl : BaseDataBoundControl
51 DataSourceSelectArguments selectArguments;
52 DataSourceView currentView;
54 protected DataBoundControl ()
58 /* Used for controls that used to inherit from
59 * WebControl, so the tag can propagate upwards
61 internal DataBoundControl (HtmlTextWriterTag tag) : base (tag)
65 protected virtual IDataSource GetDataSource ()
67 if (IsBoundUsingDataSourceID) {
69 Control ctrl = FindDataSource ();
72 throw new HttpException (string.Format ("A control with ID '{0}' could not be found.", DataSourceID));
73 if (!(ctrl is IDataSource))
74 throw new HttpException (string.Format ("The control with ID '{0}' is not a control of type IDataSource.", DataSourceID));
75 return (IDataSource) ctrl;
78 IDataSource ds = DataSource as IDataSource;
79 if (ds != null) return ds;
81 IEnumerable ie = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
82 return new CollectionDataSource (ie);
85 protected virtual DataSourceView GetData ()
87 if (currentView == null)
92 DataSourceView InternalGetData ()
94 if (currentView != null)
97 if (DataSource != null && IsBoundUsingDataSourceID)
98 throw new HttpException ("Control bound using both DataSourceID and DataSource properties.");
100 IDataSource ds = GetDataSource ();
102 return ds.GetView (DataMember);
107 protected override void OnDataPropertyChanged ()
109 base.OnDataPropertyChanged ();
113 protected virtual void OnDataSourceViewChanged (object sender, EventArgs e)
115 RequiresDataBinding = true;
118 // MSDN: The OnPagePreLoad method is overridden by the DataBoundControl class
119 // to set the BaseDataBoundControl.RequiresDataBinding property to true in
120 // cases where the HTTP request is a postback and view state is enabled but
121 // the data-bound control has not yet been bound.
123 // LAMESPEC: RequiresDataBinding is also set when http request is NOT a postback -
124 // no matter whether view state is enabled. The correct description should be:
126 // The OnPagePreLoad method is override by the DataBoundControl class
127 // to set the BaseDataBoundControl.RequiresDataBinding property to true in
128 // cases where the HTTP request is not a postback or it is a postback and view state
129 // is enabled but the data-bound control has not yet been bound.
130 protected override void OnPagePreLoad (object sender, EventArgs e)
132 base.OnPagePreLoad (sender, e);
141 // LAMESPEC: see the comment above OnPagePreLoad
142 if (!page.IsPostBack || (!IsDataBound && IsViewStateEnabled))
143 RequiresDataBinding = true;
148 void UpdateViewData ()
150 if (currentView != null)
151 currentView.DataSourceViewChanged -= new EventHandler (OnDataSourceViewChanged);
153 DataSourceView view = InternalGetData ();
154 if (view != currentView)
157 if (currentView != null)
158 currentView.DataSourceViewChanged += new EventHandler (OnDataSourceViewChanged);
161 protected internal override void OnLoad (EventArgs e)
167 // MSDN: The ConfirmInitState method sets the initialized state of the data-bound
168 // control. The method is called by the DataBoundControl class in its OnLoad
176 protected internal virtual void PerformDataBinding (IEnumerable data)
180 protected override void ValidateDataSource (object dataSource)
182 if (dataSource == null || dataSource is IListSource || dataSource is IEnumerable || dataSource is IDataSource)
184 throw new ArgumentException ("Invalid data source source type. The data source must be of type IListSource, IEnumerable or IDataSource.");
187 [ThemeableAttribute (false)]
188 [DefaultValueAttribute ("")]
189 [WebCategoryAttribute ("Data")]
190 public virtual string DataMember {
191 get { return ViewState.GetString ("DataMember", ""); }
192 set { ViewState["DataMember"] = value; }
195 [IDReferencePropertyAttribute (typeof(DataSourceControl))]
196 public override string DataSourceID {
197 get { return ViewState.GetString ("DataSourceID", ""); }
199 ViewState ["DataSourceID"] = value;
200 base.DataSourceID = value;
206 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
211 IDataSource DataSourceObject {
212 get { return GetDataSource (); }
216 // See DataBoundControl.MarkAsDataBound msdn doc for the code example
218 protected override void PerformSelect ()
220 // Call OnDataBinding here if bound to a data source using the
221 // DataSource property (instead of a DataSourceID), because the
222 // databinding statement is evaluated before the call to GetData.
223 if (!IsBoundUsingDataSourceID)
224 OnDataBinding (EventArgs.Empty);
226 // prevent recursive calls
227 RequiresDataBinding = false;
228 SelectArguments = CreateDataSourceSelectArguments ();
229 GetData ().Select (SelectArguments, new DataSourceViewSelectCallback (OnSelect));
231 // The PerformDataBinding method has completed.
234 // Raise the DataBound event.
235 OnDataBound (EventArgs.Empty);
238 void OnSelect (IEnumerable data)
240 // Call OnDataBinding only if it has not already been
241 // called in the PerformSelect method.
242 if (IsBoundUsingDataSourceID) {
243 OnDataBinding (EventArgs.Empty);
245 // The PerformDataBinding method binds the data in the
246 // retrievedData collection to elements of the data-bound control.
247 InternalPerformDataBinding (data);
255 void InternalPerformDataBinding (IEnumerable data)
257 DataBoundControlAdapter adapter = Adapter as DataBoundControlAdapter;
259 adapter.PerformDataBinding (data);
261 PerformDataBinding (data);
264 protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments ()
266 return DataSourceSelectArguments.Empty;
269 protected DataSourceSelectArguments SelectArguments {
271 if (selectArguments == null)
272 selectArguments = CreateDataSourceSelectArguments ();
273 return selectArguments;
276 selectArguments = value;
282 object dataBound = ViewState ["DataBound"];
283 return dataBound != null ? (bool) dataBound : false;
286 ViewState ["DataBound"] = value;
290 protected void MarkAsDataBound ()