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)
66 protected virtual IDataSource GetDataSource ()
68 if (IsBoundUsingDataSourceID) {
70 Control ctrl = FindDataSource ();
73 throw new HttpException (string.Format ("A control with ID '{0}' could not be found.", DataSourceID));
74 if (!(ctrl is IDataSource))
75 throw new HttpException (string.Format ("The control with ID '{0}' is not a control of type IDataSource.", DataSourceID));
76 return (IDataSource) ctrl;
79 IDataSource ds = DataSource as IDataSource;
80 if (ds != null) return ds;
82 IEnumerable ie = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
83 return new CollectionDataSource (ie);
86 protected virtual DataSourceView GetData ()
88 if (currentView == null)
93 DataSourceView InternalGetData ()
95 if (DataSource != null && IsBoundUsingDataSourceID)
96 throw new HttpException ("Control bound using both DataSourceID and DataSource properties.");
98 IDataSource ds = GetDataSource ();
100 return ds.GetView (DataMember);
105 protected override void OnDataPropertyChanged ()
107 base.OnDataPropertyChanged ();
111 protected virtual void OnDataSourceViewChanged (object sender, EventArgs e)
113 RequiresDataBinding = true;
116 // MSDN: The OnPagePreLoad method is overridden by the DataBoundControl class
117 // to set the BaseDataBoundControl.RequiresDataBinding property to true in
118 // cases where the HTTP request is a postback and view state is enabled but
119 // the data-bound control has not yet been bound.
120 protected override void OnPagePreLoad (object sender, EventArgs e)
122 base.OnPagePreLoad (sender, e);
127 private void Initialize ()
130 RequiresDataBinding = true;
135 void UpdateViewData ()
137 DataSourceView view = InternalGetData ();
138 if (view == currentView) return;
140 if (currentView != null)
141 currentView.DataSourceViewChanged -= new EventHandler (OnDataSourceViewChanged);
146 view.DataSourceViewChanged += new EventHandler (OnDataSourceViewChanged);
149 protected internal override void OnLoad (EventArgs e)
155 // MSDN: The ConfirmInitState method sets the initialized state of the data-bound
156 // control. The method is called by the DataBoundControl class in its OnLoad method.
162 protected internal virtual void PerformDataBinding (IEnumerable data)
166 protected override void ValidateDataSource (object dataSource)
168 if (dataSource == null || dataSource is IListSource || dataSource is IEnumerable || dataSource is IDataSource)
170 throw new ArgumentException ("Invalid data source source type. The data source must be of type IListSource, IEnumerable or IDataSource.");
173 [ThemeableAttribute (false)]
174 [DefaultValueAttribute ("")]
175 [WebCategoryAttribute ("Data")]
176 public virtual string DataMember {
177 get { return ViewState.GetString ("DataMember", ""); }
178 set { ViewState["DataMember"] = value; }
181 [IDReferencePropertyAttribute (typeof(DataSourceControl))]
182 public override string DataSourceID {
183 get { return ViewState.GetString ("DataSourceID", ""); }
185 ViewState ["DataSourceID"] = value;
186 base.DataSourceID = value;
191 // See DataBoundControl.MarkAsDataBound msdn doc for the code example
193 protected override void PerformSelect ()
195 // Call OnDataBinding here if bound to a data source using the
196 // DataSource property (instead of a DataSourceID), because the
197 // databinding statement is evaluated before the call to GetData.
198 if (!IsBoundUsingDataSourceID)
199 OnDataBinding (EventArgs.Empty);
201 // prevent recursive calls
202 RequiresDataBinding = false;
203 SelectArguments = CreateDataSourceSelectArguments ();
204 GetData ().Select (SelectArguments, new DataSourceViewSelectCallback (OnSelect));
206 // The PerformDataBinding method has completed.
209 // Raise the DataBound event.
210 OnDataBound (EventArgs.Empty);
213 void OnSelect (IEnumerable data)
215 // Call OnDataBinding only if it has not already been
216 // called in the PerformSelect method.
217 if (IsBoundUsingDataSourceID) {
218 OnDataBinding (EventArgs.Empty);
220 // The PerformDataBinding method binds the data in the
221 // retrievedData collection to elements of the data-bound control.
222 InternalPerformDataBinding (data);
225 protected void InternalPerformDataBinding (IEnumerable data)
227 DataBoundControlAdapter adapter = (DataBoundControlAdapter)Adapter;
229 adapter.PerformDataBinding (data);
231 PerformDataBinding (data);
234 protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments ()
236 return DataSourceSelectArguments.Empty;
239 protected DataSourceSelectArguments SelectArguments {
241 if (selectArguments == null)
242 selectArguments = CreateDataSourceSelectArguments ();
243 return selectArguments;
246 selectArguments = value;
252 object dataBound = ViewState ["DataBound"];
253 return dataBound != null ? (bool) dataBound : false;
256 ViewState ["DataBound"] = value;
260 protected void MarkAsDataBound ()