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;
40 namespace System.Web.UI.WebControls {
42 [DesignerAttribute ("System.Web.UI.Design.WebControls.DataBoundControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43 public abstract class DataBoundControl : BaseDataBoundControl
45 DataSourceSelectArguments selectArguments;
46 DataSourceView currentView;
48 protected DataBoundControl ()
52 /* Used for controls that used to inherit from
53 * WebControl, so the tag can propagate upwards
55 internal DataBoundControl (HtmlTextWriterTag tag) : base (tag)
60 protected virtual IDataSource GetDataSource ()
62 if (IsBoundUsingDataSourceID) {
63 Control ctrl = NamingContainer.FindControl (DataSourceID);
65 throw new HttpException (string.Format ("A control with ID '{0}' could not be found.", DataSourceID));
66 if (!(ctrl is IDataSource))
67 throw new HttpException (string.Format ("The control with ID '{0}' is not a control of type IDataSource.", DataSourceID));
68 return (IDataSource) ctrl;
71 if (DataSource == null) return null;
73 IDataSource ds = DataSource as IDataSource;
74 if (ds != null) return ds;
76 IEnumerable ie = DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
77 if (ie != null) return new CollectionDataSource (ie);
79 throw new HttpException (string.Format ("Unexpected data source type: {0}", DataSource.GetType()));
82 protected virtual DataSourceView GetData ()
84 if (currentView == null)
89 DataSourceView InternalGetData ()
91 if (DataSource != null && IsBoundUsingDataSourceID)
92 throw new HttpException ("Control bound using both DataSourceID and DataSource properties.");
94 IDataSource ds = GetDataSource ();
96 return ds.GetView (DataMember);
101 protected override void OnDataPropertyChanged ()
103 base.OnDataPropertyChanged ();
107 protected virtual void OnDataSourceViewChanged (object sender, EventArgs e)
109 RequiresDataBinding = true;
112 protected override void OnPagePreLoad (object sender, EventArgs e)
114 base.OnPagePreLoad (sender, e);
118 void UpdateViewData ()
120 DataSourceView view = InternalGetData ();
121 if (view == currentView) return;
123 if (currentView != null)
124 currentView.DataSourceViewChanged -= new EventHandler (OnDataSourceViewChanged);
129 view.DataSourceViewChanged += new EventHandler (OnDataSourceViewChanged);
132 protected internal override void OnLoad (EventArgs e)
134 if (IsBoundUsingDataSourceID && (!Page.IsPostBack || !EnableViewState))
135 RequiresDataBinding = true;
140 protected internal virtual void PerformDataBinding (IEnumerable data)
144 protected override void ValidateDataSource (object dataSource)
146 if (dataSource is IListSource || dataSource is IEnumerable || dataSource is IDataSource)
148 throw new ArgumentException ("Invalid data source source type. The data source must be of type IListSource, IEnumerable or IDataSource.");
151 [ThemeableAttribute (false)]
152 [DefaultValueAttribute ("")]
153 [WebCategoryAttribute ("Data")]
154 public virtual string DataMember
157 object o = ViewState["DataMember"];
163 ViewState["DataMember"] = value;
167 [IDReferencePropertyAttribute (typeof(DataSourceControl))]
168 public override string DataSourceID {
170 object o = ViewState ["DataSourceID"];
177 ViewState ["DataSourceID"] = value;
178 base.DataSourceID = value;
182 protected override void PerformSelect ()
184 OnDataBinding (EventArgs.Empty);
185 DataSourceView view = GetData ();
187 view.Select (SelectArguments, new DataSourceViewSelectCallback (OnSelect));
190 void OnSelect (IEnumerable data)
192 PerformDataBinding (data);
193 OnDataBound (EventArgs.Empty);
196 protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments ()
198 return DataSourceSelectArguments.Empty;
201 protected DataSourceSelectArguments SelectArguments {
203 if (selectArguments == null)
204 selectArguments = CreateDataSourceSelectArguments ();
205 return selectArguments;
210 protected void MarkAsDataBound ()
212 throw new NotImplementedException ();