2 // System.Web.UI.WebControls.HierarchicalDataBoundControl.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2004-2010 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.ComponentModel;
34 using System.Web.UI.WebControls.Adapters;
36 namespace System.Web.UI.WebControls
38 [DesignerAttribute ("System.Web.UI.Design.WebControls.HierarchicalDataBoundControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
39 public abstract class HierarchicalDataBoundControl : BaseDataBoundControl
41 [IDReferencePropertyAttribute (typeof(HierarchicalDataSourceControl))]
42 public override string DataSourceID {
44 object o = ViewState ["DataSourceID"];
52 RequiresDataBinding = true;
54 ViewState ["DataSourceID"] = value;
58 protected virtual HierarchicalDataSourceView GetData (string viewPath)
60 if (DataSource != null && !String.IsNullOrEmpty (DataSourceID))
61 throw new HttpException ();
62 IHierarchicalDataSource ds = GetDataSource ();
64 return ds.GetHierarchicalView (viewPath);
66 if (DataSource is IHierarchicalEnumerable)
67 return new ReadOnlyDataSourceView ((IHierarchicalEnumerable) DataSource);
72 protected virtual IHierarchicalDataSource GetDataSource ()
74 if (IsBoundUsingDataSourceID) {
75 Control ctrl = FindDataSource ();
78 throw new HttpException (string.Format ("A control with ID '{0}' could not be found.", DataSourceID));
79 if (!(ctrl is IHierarchicalDataSource))
80 throw new HttpException (string.Format ("The control with ID '{0}' is not a control of type IHierarchicalDataSource.", DataSourceID));
81 return (IHierarchicalDataSource) ctrl;
84 return DataSource as IHierarchicalDataSource;
88 get { return ViewState.GetBool ("DataBound", false); }
89 set { ViewState ["DataBound"] = value; }
92 protected void MarkAsDataBound ()
97 protected override void OnDataPropertyChanged ()
99 RequiresDataBinding = true;
102 protected virtual void OnDataSourceChanged (object sender, EventArgs e)
104 RequiresDataBinding = true;
107 protected internal override void OnLoad (EventArgs e)
119 if (!Page.IsPostBack || (IsViewStateEnabled && !IsDataBound))
120 RequiresDataBinding = true;
122 IHierarchicalDataSource ds = GetDataSource ();
123 if (ds != null && DataSourceID != "")
124 ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
127 protected override void OnPagePreLoad (object sender, EventArgs e)
129 base.OnPagePreLoad (sender, e);
134 protected void InternalPerformDataBinding ()
136 HierarchicalDataBoundControlAdapter adapter
137 = Adapter as HierarchicalDataBoundControlAdapter;
139 adapter.PerformDataBinding ();
141 PerformDataBinding ();
144 protected internal virtual void PerformDataBinding ()
148 protected override void PerformSelect ()
150 OnDataBinding (EventArgs.Empty);
151 InternalPerformDataBinding ();
152 // The PerformDataBinding method has completed.
153 RequiresDataBinding = false;
155 OnDataBound (EventArgs.Empty);
158 protected override void ValidateDataSource (object dataSource)
160 if (dataSource == null || dataSource is IHierarchicalDataSource || dataSource is IHierarchicalEnumerable)
162 throw new InvalidOperationException ("Invalid data source");