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.HierarchicalDataBoundControlDesigner, System.Design, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.IDesigner")]
43 public abstract class DataBoundControl : BaseDataBoundControl
45 DataSourceSelectArguments selectArguments;
46 DataSourceView currentView;
48 protected DataBoundControl ()
52 protected IDataSource GetDataSource ()
54 if (IsBoundUsingDataSourceID) {
55 Control ctrl = NamingContainer.FindControl (DataSourceID);
57 throw new HttpException (string.Format ("A control with ID '{0}' could not be found.", DataSourceID));
58 if (!(ctrl is IDataSource))
59 throw new HttpException (string.Format ("The control with ID '{0}' is not a control of type IDataSource.", DataSourceID));
60 return (IDataSource) ctrl;
63 if (DataSource == null) return null;
65 IDataSource ds = DataSource as IDataSource;
66 if (ds != null) return ds;
68 IEnumerable ie = DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
69 if (ie != null) return new CollectionDataSource (ie);
71 throw new HttpException (string.Format ("Unexpected data source type: {0}", DataSource.GetType()));
74 protected DataSourceView GetData ()
76 if (currentView == null)
81 DataSourceView InternalGetData ()
83 if (DataSource != null && IsBoundUsingDataSourceID)
84 throw new HttpException ("Control bound using both DataSourceID and DataSource properties.");
86 IDataSource ds = GetDataSource ();
88 return ds.GetView (DataMember);
93 protected override void OnDataPropertyChanged ()
95 base.OnDataPropertyChanged ();
99 protected virtual void OnDataSourceViewChanged (object sender, EventArgs e)
101 RequiresDataBinding = true;
104 protected override void OnPagePreLoad (object sender, EventArgs e)
106 base.OnPagePreLoad (sender, e);
110 void UpdateViewData ()
112 DataSourceView view = InternalGetData ();
113 if (view == currentView) return;
115 if (currentView != null)
116 currentView.DataSourceViewChanged -= new EventHandler (OnDataSourceViewChanged);
121 view.DataSourceViewChanged += new EventHandler (OnDataSourceViewChanged);
124 // should be `internal protected' (why, oh WHY did they do that !?!)
125 protected override void OnLoad (EventArgs e)
127 if (IsBoundUsingDataSourceID && (!Page.IsPostBack || !EnableViewState))
128 RequiresDataBinding = true;
133 protected virtual void PerformDataBinding (IEnumerable data)
135 OnDataBinding (EventArgs.Empty);
138 protected override void ValidateDataSource (object dataSource)
140 if (dataSource is IListSource || dataSource is IEnumerable || dataSource is IDataSource)
142 throw new ArgumentException ("Invalid data source source type. The data source must be of type IListSource, IEnumerable or IDataSource.");
145 [ThemeableAttribute (false)]
146 [DefaultValueAttribute ("")]
147 [WebCategoryAttribute ("Data")]
148 public string DataMember
151 object o = ViewState["DataMember"];
157 ViewState["DataMember"] = value;
161 [IDReferencePropertyAttribute (typeof(HierarchicalDataSourceControl))]
162 public override string DataSourceID {
164 object o = ViewState ["DataSourceID"];
171 ViewState ["DataSourceID"] = value;
172 base.DataSourceID = value;
176 protected override void PerformSelect ()
178 DataSourceView view = GetData ();
180 view.Select (SelectArguments, new DataSourceViewSelectCallback (OnSelect));
183 void OnSelect (IEnumerable data)
185 PerformDataBinding (data);
188 protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments ()
190 return DataSourceSelectArguments.Empty;
193 protected DataSourceSelectArguments SelectArguments {
195 if (selectArguments == null)
196 selectArguments = CreateDataSourceSelectArguments ();
197 return selectArguments;