Add a more functional (i.e. fewer-stubs) implementation of System.Data.Linq.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / HierarchicalDataBoundControl.cs
1 //
2 // System.Web.UI.WebControls.HierarchicalDataBoundControl.cs
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // (C) 2004 Novell, Inc (http://www.novell.com)
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 #if NET_2_0
32 using System.Collections;
33 using System.ComponentModel;
34 using System.Web.UI;
35 using System.Web.UI.WebControls.Adapters;
36
37 namespace System.Web.UI.WebControls
38 {
39         [DesignerAttribute ("System.Web.UI.Design.WebControls.HierarchicalDataBoundControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
40         public abstract class HierarchicalDataBoundControl : BaseDataBoundControl
41         {
42                 [IDReferencePropertyAttribute (typeof(HierarchicalDataSourceControl))]
43                 public override string DataSourceID {
44                         get {
45                                 object o = ViewState ["DataSourceID"];
46                                 if (o != null)
47                                         return (string)o;
48                                 
49                                 return String.Empty;
50                         }
51                         set {
52                                 if (Initialized)
53                                         RequiresDataBinding = true;
54                                 
55                                 ViewState ["DataSourceID"] = value;
56                         }
57                 }
58                 
59                 protected virtual HierarchicalDataSourceView GetData (string viewPath)
60                 {
61                         if (DataSource != null && DataSourceID != "")
62                                 throw new HttpException ();     
63                         IHierarchicalDataSource ds = GetDataSource ();
64                         if (ds != null)
65                                 return ds.GetHierarchicalView (viewPath);
66                         
67                         if (DataSource is IHierarchicalEnumerable)
68                                 return new ReadOnlyDataSourceView ((IHierarchicalEnumerable) DataSource);
69                         
70                         return null;
71                 }
72                 
73                 protected virtual IHierarchicalDataSource GetDataSource ()
74                 {
75                         if (IsBoundUsingDataSourceID) {
76                                 Control ctrl = FindDataSource ();
77
78                                 if (ctrl == null)
79                                         throw new HttpException (string.Format ("A control with ID '{0}' could not be found.", DataSourceID));
80                                 if (!(ctrl is IHierarchicalDataSource))
81                                         throw new HttpException (string.Format ("The control with ID '{0}' is not a control of type IHierarchicalDataSource.", DataSourceID));
82                                 return (IHierarchicalDataSource) ctrl;
83                         }
84                         
85                         return DataSource as IHierarchicalDataSource;
86                 }
87
88                 bool IsDataBound {
89                         get {
90                                 return ViewState.GetBool ("DataBound", false);
91                         }
92                         set {
93                                 ViewState ["DataBound"] = value;
94                         }
95                 }
96
97                 protected void MarkAsDataBound ()
98                 {
99                         IsDataBound = true;
100                 }
101                 
102                 protected override void OnDataPropertyChanged ()
103                 {
104                         RequiresDataBinding = true;
105                 }
106                 
107                 protected virtual void OnDataSourceChanged (object sender, EventArgs e)
108                 {
109                         RequiresDataBinding = true;
110                 }
111
112                 protected internal override void OnLoad (EventArgs e)
113                 {
114                         if (!Initialized) {
115                                 Initialize ();
116                                 ConfirmInitState ();
117                         }
118                         
119                         base.OnLoad(e);
120                 }
121
122                 void Initialize ()
123                 {
124                         if (!Page.IsPostBack || (IsViewStateEnabled && !IsDataBound))
125                                 RequiresDataBinding = true;
126
127                         IHierarchicalDataSource ds = GetDataSource ();
128                         if (ds != null && DataSourceID != "")
129                                 ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
130                 }
131
132                 protected override void OnPagePreLoad (object sender, EventArgs e)
133                 {
134                         base.OnPagePreLoad (sender, e);
135                         
136                         Initialize ();
137                 }
138                 
139                 protected void InternalPerformDataBinding ()
140                 {
141                         HierarchicalDataBoundControlAdapter adapter 
142                                 = Adapter as HierarchicalDataBoundControlAdapter;
143                         if (adapter != null)
144                                 adapter.PerformDataBinding ();
145                         else
146                                 PerformDataBinding ();
147                 }
148                 
149                 protected internal virtual void PerformDataBinding ()
150                 {
151                 }
152                 
153                 protected override void PerformSelect ()
154                 {
155                         OnDataBinding (EventArgs.Empty);
156                         InternalPerformDataBinding ();
157                         // The PerformDataBinding method has completed.
158                         RequiresDataBinding = false;
159                         MarkAsDataBound ();
160                         OnDataBound (EventArgs.Empty);
161                 }
162                 
163                 protected override void ValidateDataSource (object dataSource)
164                 {
165                         if (dataSource == null || dataSource is IHierarchicalDataSource || dataSource is IHierarchicalEnumerable)
166                                 return;
167                         throw new InvalidOperationException ("Invalid data source");
168                 }
169         }
170 }
171 #endif
172
173